【发布时间】:2014-09-09 14:20:54
【问题描述】:
所以代码将按原样运行,但是当 firstE-fourthE 变量等于 0 时计算总变量和平均变量的计算。肯定有一种方法可以稍后在代码中重新定义它们或重新计算它们吗?对于可怕的格式和缩进,我深表歉意,这个网站非常挑剔。
#include <iostream>
using namespace std;
int main()
{
char fi = '\0', mi = '\0', li = '\0', end = '\0';
float firstE = 0,
secondE = 0,
thirdE = 0,
fourthE = 0,
total = firstE + secondE + thirdE + fourthE,
average = total / 4;
cout << "This program will calculate the average of a student's exam grades." << endl;
cout << "Please enter the first initial of the student's name: ";
cin >> fi;
cout << "Please enter the middle initial of the student's name: ";
cin >> mi;
cout << "Please enter the last initial of the student's name: ";
cin >> li;
cout << "Please enter the student's first exam score: ";
cin >> firstE;
cout << "Please enter the student's second exam score: ";
cin >> secondE;
cout << "Please enter the student's third exam score: ";
cin >> thirdE;
cout << "Please enter the student's fourth exam score: ";
cin >> fourthE;
/*float total = firstE + secondE + thirdE + fourthE,
average = total / 4;*/
cout << "Student's initials: " << fi << mi << li << endl;
cout << "Exam 1: " << firstE << endl;
cout << "Exam 2: " << secondE << endl;
cout << "Exam 3: " << thirdE << endl;
cout << "Exam 4: " << fourthE << endl;
cout << "Total: " << total << endl;
cout << "Average: " << average << endl;
cin >> end;
}
【问题讨论】:
-
看起来您需要做的只是不重新定义变量,而只需通过取消注释计算代码、删除
float声明并用分号分隔来计算(;) 而不是逗号... -
我建议在尝试编写更多代码之前先阅读一些 C++ 教程。 Try this one
-
从第一个声明块中删除
total和average并取消注释已注释掉的代码。
标签: c++ visual-studio-2010 visual-c++