Basic Do While Loop

Jun 5, 2013 at 8:33am
Hey, I'm trying to write some basic Do while loops. Have done a couple this morning and all worked fine, but for some reason I cant get this one working properly.. I just cant figure out why I'm sure its set up exactly as my previous ones. Please someone help me I don't really like do while loops but I'm trying to understand them.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>

using namespace std;

int main(){
	string choice;

	do{
		cout<<"Please select one of the following: "<<endl;
		cout<<"1. Steak"<<endl;
		cout<<"2. Pork"<<endl;
		cout<<"3. Fish"<<endl<<endl;
		cin>>choice;
		cout<<endl;
	} while ((choice!="Steak") || (choice!="Pork") || (choice!="Fish"));
	cout<<"Great choice!";
}


Thanks
Last edited on Jun 5, 2013 at 8:45am
Jun 5, 2013 at 8:45am
your condition on line 16 is always true.

try this:
} while (!((choice=="steak") || (choice=="pork") || (choice=="fish")));

or this:
} while ((choice!="steak") && (choice!="pork") && (choice!="fish"));

Jun 5, 2013 at 8:59am
if you want the code to continue asking for a dish until one of the dishes is selected you should change the || for && (or "and")

 
while ((choice!="steak") and (choice!="pork") and (choice!="fish"));
Last edited on Jun 5, 2013 at 9:04am
Jun 5, 2013 at 9:02am
Ahh Thanks coder777. I thought it was just going to be something stupid like that I just couldn't figure it out.. Looks like I'm going to have to practice the difference between my &&s and ||s.

Cheers mate.
Jun 5, 2013 at 9:06am
use "or" instead of "||" and "and" instead of "&&"

it will be easier for you
Jun 5, 2013 at 9:07am
^ Cheers mate, ill try that from now on.
Topic archived. No new replies allowed.