Q:

Visual Studio generates "Error: uninitialized local variable 'x' "and online Compiler does not - Why?

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;

}
The visual studio compiler says its "uninitialized local variable 'x' used" at line 13.
However, it has no problem on other online compiler.

A:

Visual Studio is being kind to you here: the behaviour of reading an uninitialised variable is undefined in C++, and (x != -999) is an uninitialised read on the first encounter.
Don't ignore warnings issued by the other compilers.

