我正在尝试创建一个简单的 GPA 计算器,它会提示用户输入课程数量(使用新的)。接下来是一个取决于课程数量的 for 循环,要求用户输入课程的成绩和学分数量。程序完成循环和错误。请帮忙。这是代码(我第一次使用这个论坛网站顺便说一句):
#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;
}
抱歉缩进不佳(使用 Dev C++)