Here I set up a while loop where I input a selection number 1 or 2. If I choose 1 or 2 it couts the 1 or 2 whatever I choose; if I choose a number below 1 or above 2 it goes to the else statement which asks the user to input again. But if the user inputs the problem is that it won’t loop back to the beginning if they choose the right number 1 or 2; it will just end the loop there and not go to the correct if clause. Why is this? I feel as though I attacked it right.
int menus (int selection)
{
while ( selection > 2 || selection < 1)
{
if (selection == 1)
{
cout << " 1 " << endl;
}
if (selection == 2)
{
cout << " 2 " << endl;
}
else
cout << "You didnt choose 2 or 3. Choose again";
cin >> selection;
}
return 0;
}
If you enter 1 or 2, it won’t even enter the loop as neither 1 or 2 are either bigger than 2 or smaller than 1, they are equal.
This should fix it:
Note that you should also use an else if statement at the second if, it doesnt work correctly the other way.