C ++ program will not enter a loop

Ok, so here is the part of my code that gives me the problem. What I want to do is to take an estimate, decide if it is a valid estimate, and then continue to request a valid number if it is not. However, it won't even go into the loop, so ... any tips? I programmed a bit, but I'm still pretty new, so the additional explanations are great! Also, this is my first experience using boolean elements in a for loop.

for (bool b_valid=false; b_valid=false ; ) { cin >> n_grade; b_valid = true; if (n_grade>100 || n_grade<0) { cout << "Invalid grade: Re-enter a number between 0-100 : " << endl; cin >> n_grade; b_valid = false; } } 
+4
source share
7 answers

Your condition is the assignment: b_valid=false . It will evaluate to false and the loop will never execute. You meant

 for(bool b_valid = false; b_valid == false; ) ^^^^ 

There is a coding style that provides that the constant in comparison is the first argument, for example if(false == b_valid) . In this case, if you accidentally typed =, you will get a compiler error. In any case, many compilers give a warning in cases where you had a written task in which a Boolean expression was output. Either it wasn’t that difficult for you, or you just ignored the warning.

+15
source

I respectfully disagree with the other (stubbornly stubborn) answers.

Do not compare boolean with true or false .

This is pointless, redundant and leads to errors (as in your case).

Just check the value of the value itself. That is, write ! b_valid ! b_valid .

Also, using a for loop here is clearly misleading. Use while instead:

 while (! b_valid) { … } 
+11
source

You used the assignment in the loop condition ('='), it should be "==":

 for (bool b_valid=false; false==b_valid; ) 
+8
source
 for( a; b; c ) { body; } 

matches (except area) as

 a; while (b) { body; c; } 

Do this for your code:

 bool b_valid=false; while (b_valid = false) { // uh-oh 

We do not need to go further. This is a task, not a comparison. It sets b_valid to false, and then checks to see if it is true. Since it never was, the cycle never starts.

But you really need a do / while :

 bool b_valid; do { cin >> n_grade; if ( n_grade>100 || n_grade<0 ) { cout << "Invalid grade: Re-enter a number between 0-100 : " << endl; // let the next pass through the loop re-read n_grade b_valid = false; } else { b_valid = true; } } while (!b_valid); 

The do-while loop always runs at least once.

+3
source

The comparison operator is == . Now your condition is b_valid=false , which returns false .

+2
source

= - assignment operator == - comparison operator

So your loop limit does not check anything ...

Alternatively use a while loop:

 bool b_valid = false; cin >> n_grade; while(!b_valid){ if(n_grade>100 || n_grade<0) { b_valid = true; } else { cout << "Invalid grade: Re-enter a number between 0-100 : " << endl; cin >> n_grade; } } 
+1
source

You can simply convert your code into a while loop. For instance:

 bool b_valid = false; while( !b_valid ) { cin >> n_grade; b_valid = true; if (n_grade>100 || n_grade<0) { cout << "Invalid grade: Re-enter a number between 0-100 : " << endl; cin >> n_grade; b_valid = false; } } 
+1
source

All Articles