Apr 10, 2008 at 3:53am UTC
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;
Apr 10, 2008 at 11:54am UTC
You want
[code]
if( choice != 1 && choice != 2 )
/code]
and
[code]
while( choice != 1 && choice != 2 );
/code]
Apr 10, 2008 at 7:17pm UTC
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 Apr 10, 2008 at 7:25pm UTC