Assistance with Basic C++ Based GPA calculator and cin usage
I am trying to create a simple GPA calculator which prompts the user to enter the number of courses (using new) . This is followed by a for loop dependent on the number of courses, asking the user to enter the grade and number of credits for the class. The program completes the loop and errors. Please help. Here is the code (my first time using this forum site btw):
#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 (开发者_StackOverflow社区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;
}
Sorry for the poor indentation (using Dev C++)
There are a number of issues with your code, but don't think I'm being discouraging by pointing them out. Assuming you're a relative beginner, this is pretty good.
The main problem is with the following line:
cumulativeScore += gradesOfPerson;
. You've declared cumulativeScore as a pointer; it holds the address of the data you're interested in, rather than the data itself. You should either change this to
*cumulativeScore += gradeOfPerson
or make cumulativeScore an integer variable (and change all the places where you use it as a pointer).
Another key mistake is in your switch statement. Instead of something like:
case 4: if (grade == 'D') {
// logic to execute if grade is 'D'
}
break;
do this:
case 'D':
// logic to execute if grade is 'D'
break;
Next, you need to initialize *cumulativeScore
to 0, because at the beginning of execution it can contain anything.
Finally, *score
should be a numeric type, not a character. The value of the character '4'
, interpreted as a number, is not in fact 4
, which is causing errors because you treat it as such. For reference, see the list of ASCII character codes here: http://www.asciitable.com/
As for the other issues (which don't actually cause your program to fail, but aren't best practices):
- Your use of pointers is strange -- why not simply allocate normal variables? You did this with
grade
,gradesOfPerson
,i
, andj
, so you clearly know how. Why did you choose to make the rest of your variables pointers? - Don't include
conio.h
. First of all, you're not using any of the functions declared there. Second, it's non-standard and not available on most platforms. - You should do more error handling: is the user entering sensible grade letters? Are you making sure the number of courses they enter is positive? And so on.
Finally, just as a note, you can get the Express version of Microsoft Visual C++ for free. It's actively maintained and it's light-years ahead of Dev-C++ (for one, it helps you indent code correctly! :D)
Good luck!
On this line:
cumulativeScore += gradesOfPerson;
cumulativeScore is a pointer, so this line moves where it is pointing. You probably want to write:
*cumulativeScore += gradesOfPerson;
The reason it was crashing is because you were moving where cumulativeScore was pointing, and later on when you tried to dereference it, it was pointing at invalid memory.
Without seeing the error it's hard to say what's wrong, but on first glance, if the gpa
line ( int gpa = (*cumulativeScore)/(*numberOfCourses);
) isn't throwing an error now, it may in the future. gpa
should be a double
.
As an aside, your delete
statement is badly broken. You can't delete multiple variables in one go, this
delete numberOfCourses, credits, score, cumulativeScore;
is actually using the C++ comma operator which discards its left-hand side, so only cumulativeScore
gets deallocated.
精彩评论