In the online compiler, the code ( http://cpp.sh/4gnfb ) as you type:

4 1 2 3 4 

displays YES , and in Visual Studio displays NO .
In short, the code should output YES , if there is 0 among the N numbers. I know that it could be written more simply and concisely, the problem of different output is important to me. What could be the problem? In the online compiler, I poked different versions of c ++, the result is the same.

Code below:

 int main() { int N, i = 0, a; cin >> N; bool NovyiGod2018; while (i < N) { cin >> a; if (a == 0) { NovyiGod2018 = 1; } else if (NovyiGod2018 != 1) { NovyiGod2018 = 0; } ++i; } if (NovyiGod2018 == 1) { cout << "YES"; } else { cout << "NO"; } } 
  • 3
    You should pay attention to the compiler warnings. The studio directly issues a warning C4701: potentially uninitialized local variable 'NovyiGod2018' used - VTT

1 answer 1

The problem is in the uninitialized variable bool NovyiGod2018; Fix on

 bool NovyiGod2018 = false; 

in your online compiler, and get NO .

And - for this, and the bool type, in order to have true / false values ​​- it is better to use these values ​​for it, and not 0/1 ...