Looping same statement

I want to loop the same statement "Please enter a number other than 5" over and over again unless they enter the number 5, that's when I want to break out of it.

Is this the best way to do it?

1
2
3
4
5
6
7
8
9
10
  //ask user to enter a number except for 5, if number is not 5 keep asking

		cout << "Please enter a number other than 5." << endl;
		cin >> x;

		do
		{
			cout << "Please enter a number other than 5." << endl;
			cin >> x;
		} while (x > 5 && x < 5);
instead of (x > 5 && x < 5) do (x!=5) it means x does not equal 5
Last edited on
You could simply do. while(x != 5)

Another method since you have it do the same output/input and no error check would be:
1
2
3
4
while( std::cout << "Please enter a number other than 5: " && std::cin >> x && x != 5)
{
    //do something with number
}
Topic archived. No new replies allowed.