你能告诉我错误在哪里以及为什么当我执行程序时会显示随机数?
#include <iostream>
using namespace std;
int main() {
int total;
cout << "Please enter a number: ";
double x1;
cin >> x1;
total = total + x1;
cout << "total: " << total << endl;
cout << "Please enter a number: ";
double x2;
cin >> x2;
total = total + x2;
cout << "total: " << total << endl;
total = total / 2;
cout << "total: " << total << endl;
cout << "The average is " << total << endl;
return 0;
}
答案 0 :(得分:2)
变量total
未初始化。必须在使用之前先将其初始化,如下所示:
int total = 0;