amgtree amgtree - 27 days ago 8
C++ Question

This loop terminates even though the condition is still met

Hello this is my second question for the same c++ program (sorry if this is a simple issue)

I am trying to use a loop to check if a input was valid. However this loop doesn't seem to respect if the input was valid or not.

int b(-1);
check = false;
while (b < 1 || b > 12)
{
if (check)
std::cout<<"Sorry try again!\nMonth: ";
std::cin>>b;
check = true;
} // I had this as {


If the user inputs a value between 1 and 12. Otherwise continue with the loop.

However when I run the program and input a non-valid input it continues on.

Year: 2017
Month *In number format*: 20
Day:


Even -1 doesn't work and that is weird because I set the value as -1 before the loop starts.

Year: 2017
Month *In number format*: -1
Day:


Is it the use of the or statement with less than and greater than?

Solved because I accidentally inputted the wrong curly bracket.

Answer Source

I cant see the end of your while loop. Please post the complete while loop. Provided logic seem to be good.