2011-06-24 69 views
3

我想創建一個簡單的GPA計算器,它提示用戶輸入課程數量(使用新的)。接下來是依賴於課程數量的for循環,要求用戶輸入班級的學分和學分數。該程序完成循環和錯誤。請幫忙。下面是代碼(我第一次使用該論壇網站的BTW):幫助使用基於C++的GPA計算器和cin用法

#include <iostream> 
#include <conio.h> 
using namespace std; 

int main(){ 
    cout<<"Welcome to the GPA calculator"; 
    cout <<endl; 
    cout<<"Please enter the number of courses you wish to calculate : "; 
    int*numberOfCourses = new int; 
    cin>>*numberOfCourses; //must dereference as it is a pointer and I AM SETTING variable. 
    char grade, *credits= new char; 
    int gradesOfPerson = 0; 
    int*score = new int; 
    int j = 0; 
    int i = 0; 
    int*cumulativeScore= new int; 

    while(i< *numberOfCourses){ 
    cout <<"Please enter the credits of your " <<(i+1) <<" course. " ; 
    cin >>*credits; 
    cin.get(); 
    cout << "Please enter your grade :"; 
    cin>>(grade); 
    cout <<endl; 
    switch (grade){ 
    case 1: if (grade=='A'){ 
     *score = 4; 
     break; } 
    case 2: if (grade=='B'){ 
     *score = 3; 
     break; } 
    case 3: if (grade=='C'){ 
     *score = 2; 
     break; } 
    case 4: if (grade=='D'){ 
     *score = 1; 
     break; } 

    case 5: if (grade=='D'){ 
     *score = 1; 
     break; } 
    case 6: if (grade =='E'){ 
     *score = 0; 
     break; 
     } 
     } 
    gradesOfPerson = ((*score)*(*credits)); 
    cumulativeScore += gradesOfPerson; 
    i++; 
    } 
int gpa = (*cumulativeScore)/(*numberOfCourses); 
cout <<"Your GPA is : " <<gpa; 
delete numberOfCourses, credits, score, cumulativeScore; 
} 

對不起,劣質壓痕(使用開發的C++)

+0

這個項目不需要動態分配('new')。如果你需要一次存儲所有分數,你需要一個動態數組(儘管最好讓'std :: vector'處理細節,但爲了計算GPA,你只需要一個總計的分數點和總學分,跳過'new'和'delete'。 –

回答

0

沒有看到它的硬錯誤說什麼是錯的,但在第一次一目瞭然,如果gpa行(int gpa = (*cumulativeScore)/(*numberOfCourses);)現在不會拋出錯誤,它可能在將來。 gpa應該是double

2

你的代碼有很多問題,但不要以爲我會因爲指出他們而感到灰心。假設你是一個相對的初學者,這非常好。

的主要問題是使用以下行:

cumulativeScore += gradesOfPerson; 

。您已將cumulativeScore聲明爲指針;它包含您感興趣的數據的地址,而不是數據本身。您應該改變這

*cumulativeScore += gradeOfPerson 

或使cumulativeScore整數變量(和更改所有,你把它作爲一個指針的位置)。

另一個關鍵錯誤是您的切換語句。取而代之的是這樣的:

case 4: if (grade == 'D') { 
// logic to execute if grade is 'D' 
} 
break; 

做到這一點:

case 'D': 
// logic to execute if grade is 'D' 
break; 

接下來,你需要初始化*cumulativeScore爲0,因爲在執行它可以包含任何內容的開始。最後,*score應該是數字類型,而不是字符。字符'4'的值被解釋爲一個數字,實際上並不是4,因爲您將其視爲這樣會導致錯誤。僅供參考,請參閱的ASCII字符代碼這裏的列表:http://www.asciitable.com/

至於其他的問題(這實際上並不導致程序失敗,但不是最好的做法):

  • 您在使用指針很奇怪 - 爲什麼不簡單地分配正常變量?你用gradegradesOfPerson,ij這樣做了,所以你清楚知道如何。你爲什麼選擇製作其餘的變量指針?
  • 不包括conio.h。首先,你沒有使用在那裏聲明的任何函數。其次,它是非標準的,在大多數平臺上都不可用。
  • 你應該做更多的錯誤處理:用戶輸入明智的等級字母嗎?你確定他們輸入的課程數量是正面的嗎?等等。

最後,就像說明一樣,您可以免費獲得Express版本的Microsoft Visual C++。它是積極維護的,它比Dev-C++早了幾年(其中之一,它可以幫助你正確縮進代碼!:D)

祝你好運!

1

在此行中:

cumulativeScore += gradesOfPerson; 

cumulativeScore是一個指針,所以這條線移動,其中它指向。你可能想要寫:

*cumulativeScore += gradesOfPerson; 

它崩潰的原因是因爲你在,當你試圖取消對它的引用,它在無效的內存指着其中移動cumulativeScore指着,後來。

0

另外,您的delete聲明已嚴重損壞。你不能一次過刪除多個變量,這

delete numberOfCourses, credits, score, cumulativeScore; 

實際上是使用C++的逗號操作符其拋棄它的左側,所以只有cumulativeScore得到釋放。