Why doesn't this code display what I think it should?
I'm stuck and everything is correct except the Grade wont display, any help would be appreciated!
#include <iostream>
using namespace std;
void calcScore(int grade, double&total);
int main()
{
//declare variables
int score = 0;
int totalPoints = 0; //accumulator
char grade = ' ';
//get first score
cout << "First score (-1 to stop): ";
cin >> score;
while (score != -1)
{
//update accumulator, then get another score
totalPoints += score;
cout << "Next score (-1 to stop): ";
cin >> score;
} //end while
//display the total points and grade
cout << "Total points earned: " << totalPoints << endl;
开发者_如何学运维 cout << "Grade: " << grade << endl;
return 0;
} //end of main function
void calcScore(int grade, double & total)
{
if (total >= 315)
grade = 'A';
else if (total >= 280)
grade = 'B';
else if (total >= 245)
grade = 'C';
else if (total >= 210)
grade = 'D';
else
grade = 'F';
}
You've tagged this as homework, so I won't show you a code solution. A couple of things for you to look at:
- Your
calcScore
function is never being called anywhere. - You're passing
grade
tocalcScore
by value, so you'll be operating upon a local copy ofgrade
within yourcalcScore
function. Is that really what you want? - You've declared
grade
aschar
in main, but as anint
in thecalcScore
declaration. Do you understand the relationship between the two?
Good luck! :)
精彩评论