代码如下:
// WAP to implement student result preparation.
#include "stdafx.h"
#include "iostream"
#include "conio.h"
using namespace std;
float total;
const int obj = 3;
const int size = 3;
class student
{
int rollno;
char name[20], grade;
float marks[size], perc;
public:
void getval()
{
cout << "Enter roll number: ";
cin >> rollno;
cout << "\nEnter student's name: ";
cin >> name;
cout << "\nEnter student's physics marks: ";
cin >> marks[0];
cout << "\nEnter student's chemistry marks: ";
cin >> marks[1];
cout << "\nEnter student's mathematics' marks: ";
cin >> marks [2];
cout << "\n";
}
void calculate()
{
perc = total / 3;
if (perc < 50)
grade = 'F';
else if (perc >=50 && perc < 60)
grade = 'D';
else if (perc >= 60 && perc < 75)
grade = 'C';
else if (perc >= 75 && perc < 90)
grade = 'B';
else
grade = 'A';
}
void prnresult()
{
cout << "\nRoll number: " << rollno;
cout << "\nName: " << name;
cout << "\nTotal marks: " << total;
cout << "\nPercentage: " << perc;
cout << "\nGrade: " << grade;
cout << "\n";
}
};
student jk[obj];
int _tmain(int argc, _TCHAR* argv[])
{
for (int i = 0; i < obj; i++)
{ cout << "Student " << i + 1 << "\n";
jk[i].getval();
}
for (int j = 0; j < obj; j++)
{
jk[j].calculate();
cout << "Result: ";
jk[j].prnresult();
}
_getch();
return 0;
}
这是一个教科书程序。作者说它在 Turbo C++ 中完美运行(你能想象吗?上次更新是在 20 世纪 90 年代初!)。但是当我在 Visual C++ 2010 Express 中编译它时,它不会计算总数和百分比。它只是显示零。知道为什么会发生这种情况吗?
最佳答案
作者撒谎了。我在代码中找不到为 total
变量分配任何值的任何位置。因此,total
值仍为 0
(全局变量的 0
初始化)。因此 perc = Total/3;
也等于 0
。
关于c++ - Visual C++ 中的异常输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9192844/