Problem with the do...while loop

Hey !

I want to do a loop that is not closed if the user don't type 1 or 2... This is my code :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

#include <iostream>;

using namespace std;

int main()
{

	int choice;

	do
	{
		std::cout << "Type 1 or 2. \n";
		std::cin >> choice;

	} while (choice != 1 || choice != 2);

	std::cout << "Yey !";

	return 0;
}


And when i type '2' or '1' the loop is not closed :|

I need your help please !

Thanks.
-Alex
Line 16: You want the && condition, not ||.

If choice is 2, then choice!=1 is true and your loop continues.
Likewise if choice is 1, then choice!=2 is true and your loop continues.
Last edited on
But if i type

int x;

do
{
std::cout << "Type 1 or 2 to exit the loop";
}
while (x != 1 && x != 2)

If the user type '1' will be true but for 2 no, so the loop continue ?

EDIT:

I mean :

The compiler : ok, while x is not egal to 1 or x is not egal to 2, i continue my loop but if x is egal to 1 or 2 i go out the loop !

Last edited on
 
while (! (x == 1 || x == 2))


Yeah ! Thanks !

But logycally, it is the same thing as while (x != 1 || x != 2) no ? :\
No.

The order of evaluation makes a difference.
Ok! Thanks for your time AbstactionAnon :)
Topic archived. No new replies allowed.