While Loop With If/Else Statement

My if/else statement isn't working properly within my while loop. If the user enters in a number NOT between 1 and 9 then the while statement should work and it should ask the user to re-enter a number but it's not?

int positionNumber = 1;
cout << "Please Enter A Position Number (1-9) To Place The X: ";
cin >> positionNumber;
while (positionNumber>=1 && positionNumber<=9)
{
if (positionNumber >= 1 && positionNumber <= 9) break;
else cout << "Please Enter A Position Number (1-9) To Place The X: ";

}



return 0;
Last edited on
So your if and else statement aren't in the right position. So the if statement is already in play as its also the while loop.
So you could try something like
1
2
3
4
5
6
7
8
9
10
11
12
 
do 
{
if (positionNumber >=1 && positionNumber <=9)
{
break;
}
else;
{
cout << "Please enter a Positon Number (1-9) to place the X:";
}
}while(positionNumber <=9)
Last edited on
also put the code into the code Format. ( it looks like <> in a little box when you are typing)


but dmete's answer should work. Good Luck dude!
Last edited on
Topic archived. No new replies allowed.