While loop wont end

Mar 18, 2013 at 10:08am
I've been trying to make a simple console game, and for whatever reason a while loop broke on me. I could swear this is the same code that was working ten minutes earlier.

1
2
3
4
5
6
7
8
cout << "Are you a male or female?: ";
	cin >> sex;
	while(sex != "male" || "Male" || "MALE" || "female" || "Female" || "FEMALE")
	{
		cout << "I'm sorry, my hearing must be leaving me. Could you repeat that?" << endl;
		cout << "Are you a male or female?: ";
		cin >> sex;
	}


For the life of me I can't figure out why this quit working. If i change it to
1
2
3
4
5
6
7
8
cout << "Are you a male or female?: ";
	cin >> sex;
	while(sex != "male")
	{
		cout << "I'm sorry, my hearing must be leaving me. Could you repeat that?" << endl;
		cout << "Are you a male or female?: ";
		cin >> sex;
	}

it executes properly, but then the program doesn't do the desired task.

Thanks in advance.
Mar 18, 2013 at 10:21am
while( (sex != "male") || (sex !="Male") || (sex !="MALE") //...
In addition this will work only if sex is std::string!
Last edited on Mar 18, 2013 at 10:22am
Mar 18, 2013 at 10:26am
@KatuXiK

I don't think it is necessary to try and allow for every possible combination of input.

Most people would ask for M or F or T (if you wanted to allow for trans-gender)

So you can use the toupper function to convert to upper case, then use a switch statement to carry out the appropriate action. Put a default: clause in to catch any bad input.

Don't forget to look at the reference section at the top left of this page.

Hope all goes well.
Mar 18, 2013 at 10:40am
@MiiNiPaa

I have the string defined, and it was working before, but for some reason the loops just runs continually.

@TheIdeasMan

Alright, what I got out of your post is that I need to look up "toupper" and "default: clause" haha. I'll check the references, thanks.

Really what I'm doing is just setting up a project so I can learn as I go, when I want the program to do something I don't know how to do, I'll figure it out! Thing is, I thought I knew how to do this haha.
Mar 18, 2013 at 10:48am
You will find the default clause under "switch" in the control section of the tutorial.

Alternatively just Google these - will probably take you to the same place:

C++ switch

C++ toupper

cppreference is a good site too.
Topic archived. No new replies allowed.