Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 声明函数第二部分_C++ - Fatal编程技术网

C++ 声明函数第二部分

C++ 声明函数第二部分,c++,C++,伙计们,请再帮我一次我的计划。我改变了里面代码的顺序。请检查我的代码有什么问题。它运行,但不执行它应该执行的任务。它应该计算用户输入的成绩总数,并显示相应的备注。不幸的是,它不起作用:请帮帮我 #include<iostream> #include<conio.h> using namespace std; void computePG(int& PG); void Remark(int PG); int x, y, z, w, p; int

伙计们,请再帮我一次我的计划。我改变了里面代码的顺序。请检查我的代码有什么问题。它运行,但不执行它应该执行的任务。它应该计算用户输入的成绩总数,并显示相应的备注。不幸的是,它不起作用:请帮帮我

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

using namespace std;

void computePG(int& PG);
void Remark(int PG);

    int x, y, z, w, p;
    int prelimGrade,yourRemark,PG;
    int preliminaryGrade; 

int main()
{
    int pGrade;

 cout<<"Programmed by: Katrina G. Gozo, 1ISC";
 cout<<endl<<"\nDate: Aug. 23,2013";
 cout<<endl<<"\nThis program intends to compute the PG and make the necessary remarks";

 cout<<"\n\nPlease enter your score on quiz 1 ";
 cin>>x;

 cout<<"\nPlease enter your score on quiz 2 ";
 cin>>y;

 cout<<"\nPlease enter your score on quiz 3 ";
 cin>>z;

 cout<<"\nPlease enter your score on prelims ";
 cin>>p;


computePG(pGrade);
Remark(pGrade);


getch();
}

void computePG(int& PG)
{
    PG = x/30 * 20 + y/50 * 20 + z/40 * 20 + w/100 * 40;
    cout << "\nYour prelim grade is " << PG;

} 

void Remark(int PG)
{
     if (PG>=90)
        cout<<"A." <<endl;
     else if (PG>=80)
          cout<<"B."<<endl;
     else if (PG>=70)
          cout<<"C."<<endl;
     else if (PG>=60)
          cout<<"D."<<endl;
     else 
          cout<<"E."<<endl;
}

您很可能与整数运算有冲突。注意:将一个整数除以另一个整数时,将得到一个整数结果,该结果四舍五入为零


因此,您需要使用double作为PG和pGrade的类型,并在computePG浮点数中生成常量,方法是将它们写成30.0、20.0等。

您可能应该为变量PG使用double,这样您就有足够的十进制精度


此外,您可能希望在将来避免使用全局变量,因为我猜这是您如何产生此错误的-在使用w之前,您从未为w赋值,这意味着编译器将其赋值为0,这可能是破坏结果的原因。

Define不起作用。是时候学习如何使用调试程序了。我希望输出中是您的名字!你期望它做什么?它做什么?例如,29/30*20是0。OTOH,29*20/30是19。请进一步说明,先生,我只是一个整数,一个整数只关心数字的整部分,所以除法时可能会有舍入错误,即使你不关心小数点后的数字。例如,如果要使用int i=1/2;,我将等于0,而不是1,因为它只会降低0.5。正如我在回答中提到的,使用双元将解决这个问题,但我建议您熟悉C++中的不同数值类型。使用双元的@ NAMUFAK是不够的,因为整个计算表达式仍然在整数级。她还必须通过在每个常数后面加上.0来将所有这些常数转换为双倍。w是一个全局的,因此是0-初始化的;它的用途实际上是定义明确的,对计算没有任何贡献。当然,这并不意味着我支持globals。我从来都不知道,这说明我使用globals的程度。我会更新我的答案。如果她希望w有一个有用的值,这可能仍然是她的问题。