While loop not working in program with boolean (C++) -
hi writing simple program asks user input within range. used boolean start while loop, when try declaring boolean false in first if statement, doesn't end loop , program keeps asking user input. i've thought using break shouldn't negating condition stop loop?
int num; cout << "enter number"; bool invnum= true; while (invnum = true) { cin >> num; if (num >= 0 && num <= 20) { cout << "you typed: " << num << endl; invnum = false; //break; } if (num <= 0 || num >= 20) { cout << "type number: "; } } return exit_success; }
=
not used comparison. should use ==
. change while (invnum = true)
while (invnum == true)
or better write while (true == invnum)
Comments
Post a Comment