gpt4 book ai didi

c++ - Visual C++ 中的奇怪输出

转载 作者:搜寻专家 更新时间:2023-10-31 01:16:19 27 4
gpt4 key购买 nike

代码如下:

// 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++ 中完美运行(你能想象吗?上次更新是在 1990 年代初期!)。但是当我在 Visual C++ 2010 Express 中编译它时,它不计算总数和百分比。它只是显示为零。知道为什么会这样吗?

最佳答案

作者撒谎。我在代码中找不到将任何值分配给 total 变量的任何位置。所以,total 值仍然是0(0 初始化全局变量)。因此 perc = total/3; 也等于 0

关于c++ - Visual C++ 中的奇怪输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9192844/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com