Таким образом, код as is будет запущен, однако вычисления для общей и средней переменных вычисляются, когда переменные firstE-4thE равны 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' декларации и отделив точкой с запятой (';') вместо запятой ... – abiessu
Я бы посоветовал пройти несколько учебных пособий на C++, прежде чем писать больше кода. [Попробуйте это] (http://www.learncpp.com/) – TartanLlama
Удалите 'total' и' average' из первого блока объявлений и раскомментируйте код с комментариями. – molbdnilo