int main() {
int y, z, a, b, c;
int x;
double d;
cout << "Enter Numbers! Enter -999 to Stop:\n";
y = 0; z = 0; a = 0;
b = 0; c = 0; d = 0;
while (x != -999) {
cin >> x;
if (x > 0) y++;
if (x > 0) b += x;
if (x == 0) z++;
if (x < 0 && x != -999)a++;
if (x < 0 && x != -999) c += x;
}
d = b * 1.0 / y * 1.0;
if (b == 0 || y == 0) {
d = 0;
}
cout << "Total Positive Numbers are: " << y << endl;
cout << "Total Negative Numbers are: " << a << endl;
cout << "Total Zeros are: " << z << endl;
cout << "Sum of Positive Numbers is: " << b << endl;
cout << "Sum of Negative Numbers is: " << c << endl;
cout << "Average of Positive Numbers is: " << d * 1.0 << endl;
return 0;
}
Visual Studio编译器在第13行说其“使用的未初始化局部变量'x'”。
但是,在其他在线编译器上没有问题。
Visual Studio在这里对您很友好:读取未初始化变量的行为在C ++中是未定义的,并且(x != -999)
在第一次遇到时是未初始化的读取。
不要忽略其他编译器发出的警告。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句