开发者

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:

  1. Your calcScore function is never being called anywhere.
  2. You're passing grade to calcScore by value, so you'll be operating upon a local copy of grade within your calcScore function. Is that really what you want?
  3. You've declared grade as char in main, but as an int in the calcScore declaration. Do you understand the relationship between the two?

Good luck! :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜