0

Whats Wrong with C++ code?

here's the code: #include <iostream> using namespace std; int main() { int prog = 10 || 20 || 30 || 40 || 50 || 60 || 70 || 80 || 90 || 100; if(prog==10){ cout << "<=>" << 10 << "%"; } else if(prog==20){ cout << "<==>" << 20 << "%"; } else if(prog==30){ cout << "<===>" << 30 << "%"; } else if(prog==40){ cout << "<====>" << 40 << "%"; } else if(prog==50){ cout << "<=====>" << 50 << "%"; } else if(prog==60){ cout << "<======>" << 60 << "%"; } else if(prog==70){ cout << "<=======>" << 70 << "%"; } else if(prog==80){ cout << "<========>" << 80 << "%"; } else if(prog==90){ cout << "<=========>" << 90 << "%"; } else if(prog==100) { cout << "<==========>" << 100 << "%"; } return 0; } And it's outputing: No output.

3rd Oct 2018, 6:18 PM
Potato Hacker
Potato Hacker - avatar
4 Réponses
+ 2
You can't use || to assign one of several values to a variable (if that is what you're trying to do). Use a random value instead: #include <stdlib.h> #include <time.h> srand((unsigned)time(NULL)); int prog = (rand() % 10 + 1) * 10;
3rd Oct 2018, 6:49 PM
Anna
Anna - avatar
+ 2
You're welcome. Btw, if you want to make your code a little more compact: https://code.sololearn.com/c9xKZdAu8r4M/?ref=app
3rd Oct 2018, 7:16 PM
Anna
Anna - avatar
+ 2
The following expression is an evaluation sequence using logical OR operator which will be true (1) or false (0). int prog = 10 || 20 || 30 || 40 || 50 || 60 || 70 || 80 || 90 || 100; The order of evaluation is from left to right and as soon as the first non-zero number is encountered, the evaluation will be done and true is returned. The only case in which the evaluation becomes false is when all numerical constants in the sequence is zero. So, considering the above line, the following conclusion can be drawn, int prog = 10 || 20 || 30 || 40 || 50 || 60 || 70 || 80 || 90 || 100; ~~~~~~~~^ First non-zero constant evaluation stops and true is returned That's why prog variable fails to satisfy if conditions and the program gets terminated without any output.
3rd Oct 2018, 8:21 PM
Babak
Babak - avatar
+ 1
Thanks @Anna !
3rd Oct 2018, 7:06 PM
Potato Hacker
Potato Hacker - avatar