1

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

#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";
  }
4

2 回答 2

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;
  }
于 2013-08-23T10:52:15.573 回答
0

变量double& pScore不被函数更新computePG

于 2013-08-23T10:53:46.463 回答