Logical not '!=' ??

I am trying to create a do while loop with a logical not equal to statement within it for my programming class but its not working. Heres what I have:

1
2
3
4
5
6
7
8
9
10
11
12
do									
	{
		cout << "What would you like to do? \n";				
		cout << "Enter 1 to Report Gasoline Consumption. \n";
		cout << "Enter 2 to quit." << endl;
		cin >> choice;
		if(choice != 1 || choice != 2)
		{
			cout << "Invalid choice." << endl;
			cout << "Please try again." << endl << endl;
		}
	} while(choice != 1 || choice != 2);



Basically its a check to see if the input is valid. Any help is greatly appreciated.
I think the if loop inside the do-while is not needed. You can just write the program like this:
do
{
cout << "What would you like to do? \n";
cout << "Enter 1 to Report Gasoline Consumption. \n";
cout << "Enter 2 to quit." << endl;
cin >> choice;

} while(choice == 1 || choice == 2);
cout << "Invalid choice." << endl;
cout << "Please try again." << endl << endl;
You want

[code]
if( choice != 1 && choice != 2 )
/code]

and

[code]
while( choice != 1 && choice != 2 );
/code]
1
2
3
4
5
6
7
8
9
10
11
12
do {
     cout << "What would you like to do? \n";
     cout << "Enter 1 to Report Gasoline Consumption. \n";
     cout << "Enter 2 to quit." << endl;
     cin >> choice;
     if(choice != 1 && choice != 2) { //only invalid if both of these are false
          cout << "Invalid choice." << endl;
          cout << "Please try again." << endl << endl;
     } else if (choice == 1) {
          //report gasoline consumption
     }
} while(choice != 2); //you want to exit when the choice 2 is only 
Last edited on
Topic archived. No new replies allowed.