0

so if I have const int SIZE = 4; and a while loop

const int SIZE = 4;
int counter = 0;
    while(true) {
      // do something ...
      if(SIZE == counter+1) {cout << "Hello world"}
    }

why is this not working? and how is the right way to do it?

3
Contributors
3
Replies
18
Views
3 Years
Discussion Span
Last Post by nhrnjic6
0

--- while(true) will run forever
--- You don't increment your counter IN the while loop
--- Place your condition not in an if-statement, but put it in the place of true
Success!

0

the value of SIzE is 4, the initial value of counter is 0. So SIZE never equals (counter+1). The value of counter never changes inside the loop.

how is the right way to do it?

Increment counter somewhere inside the loop but outside the if statement, most likely place is on line 4.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.