声明功能

时间:2013-08-23 10:49:47

标签: c++

大家好,请看看我的程序并帮我找出它的错误。它编译并运行。程序要求用户输入成绩,输入后,它将计算总预备等级,并应显示总成绩的相应备注。但这是我的问题,相应的注释根本不显示,它只显示备注的无效输入。请帮助我。

#include<iostream>
#include<conio.h>

using namespace std;

void computePG(double& pScore);
void Remark(double pScore); 

int main()
{
  double cPrelimGrade;

  cout << "\n\n\tThis program is intended to compute the prelim grade\n";

  computePG(cPrelimGrade);
  Remark(cPrelimGrade);

getch();
}


  void computePG(double& pScore)
  {   
    double q1, q2, q3, pe, cpScore = 0;

    cout << "\n\n\tPlease enter your score in quiz 1: ";
    cin >> q1;
    cout << "\tPlease enter your score in quiz 2: ";
    cin >> q2;
    cout << "\tPlease enter your score in quiz 3: ";
    cin >> q3;
    cout << "\tPlease enter your score in prelim exam: ";
    cin >> pe;
    cpScore = ((q1/30) * 20) + ((q2/50) * 20) + ((q3/40) * 20) + ((pe/100) * 40);
    cout << "\n\n\tThe computed PG is: " << cpScore;
  }


  void Remark(double pScore)
  {    
        if (pScore<=59&&pScore>=0)
           cout << "\n\tRemark: E";   
        else if (pScore<=69&&pScore>=60)
           cout << "\n\tRemark: D";
        else if (pScore<=79&&pScore>=70)
           cout << "\n\tRemark: C";
        else if (pScore<=89&&pScore>=80)
           cout << "\n\tRemark: B";
        else if (pScore<=100&&pScore>=90)
           cout << "\n\tRemark: A";
        else
            cout << "\n\t\tInvalid input";
  }

2 个答案:

答案 0 :(得分:5)

您正在传递pScore作为参考,但您没有为其分配任何值,而是将结果存储到本地变量cpScore中:

  void computePG(double& pScore)
  {   
    double q1, q2, q3, pe, cpScore = 0;

    cout << "\n\n\tPlease enter your score in quiz 1: ";
    cin >> q1;
    cout << "\tPlease enter your score in quiz 2: ";
    cin >> q2;
    cout << "\tPlease enter your score in quiz 3: ";
    cin >> q3;
    cout << "\tPlease enter your score in prelim exam: ";
    cin >> pe;
    pScore = ((q1/30) * 20) + ((q2/50) * 20) + ((q3/40) * 20) + ((pe/100) * 40);
    cout << "\n\n\tThe computed PG is: " << pScore;
  }

答案 1 :(得分:0)

变量double& pScore未按函数computePG更新。