为什么这段代码没有显示我认为它应该是什么?

时间:2011-04-20 15:55:53

标签: c++

我被卡住了,一切都是正确的,除了等级不会显示,任何帮助都将不胜感激!

#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';
}

1 个答案:

答案 0 :(得分:6)

您已将此标记为作业,因此我不会向您展示代码解决方案。有几件事你可以看一下:

  1. 您的calcScore函数永远不会被调用。
  2. 您按价值将grade传递给calcScore,因此您将在grade功能中使用calcScore的本地副本进行操作。 Is that really what you want?
  3. 您已在main中声明gradechar,但在int声明中声明为calcScore。你了解两者之间的关系吗?
  4. 祝你好运! :)