我想創建一個簡單的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++)
這個項目不需要動態分配('new')。如果你需要一次存儲所有分數,你需要一個動態數組(儘管最好讓'std :: vector'處理細節,但爲了計算GPA,你只需要一個總計的分數點和總學分,跳過'new'和'delete'。 –