2 Replies - 940 Views - Last Post: 01 May 2009 - 11:36 AM Rate Topic: -----

#1 OsuSensei  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 01-February 09

Multiple Strings in a While Statement

Post icon  Posted 01 May 2009 - 11:28 AM

I am trying to make a program where a user can enter their character's gender when they go to create their character inside my game. I want them to be able to type either male or female, in which the program would move on to the next statement, or if they did not type male or female for an error message to come up saying that they must enter either male or female to continue.

So far, this is part of my member function that I have created:

	while ( enteredCommand != "male" )
	{
		cout << "Error. Please enter either male or female for your characters gender." << endl;
		getline ( cin, enteredCommand);
	}

	if ( enteredCommand == "male" )
	{
		cout << "Enter Your Character's Age (Between 18-60):" << endl;
		getline ( cin, enteredCommand );
		yourCharactersGender = enteredCommand;
	}
	else if (enteredCommand == "female" )
	{
		cout << "Enter Your Character's Age (Between 18-60):" << endl;
		getline ( cin, enteredCommand );
		yourCharactersGender = enteredCommand;
	}


In the code above, I have it so that if the enteredCommand is not equal to male, then it is to run a while statement that outputs an error message. If the command is equal to male, then it continues on. My problem is that I want their to be multiple values that the user can enter for the program to continue to the next statement ( those values being either male or female ).

If anyone knows how to do this, I would appreciate your help.

Thanks, Osu

This post has been edited by OsuSensei: 01 May 2009 - 11:37 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Multiple Strings in a While Statement

#2 KYA  Icon User is online

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3120
  • View blog
  • Posts: 19,163
  • Joined: 14-September 07

Re: Multiple Strings in a While Statement

Posted 01 May 2009 - 11:31 AM

Something like:

while ((enteredCommand != "male") && (enteredCommand != "female"))
{
		cout << "Error. Please enter either male or female for your characters gender." << endl;
		getline ( cin, enteredCommand);
}




?

It would be better to put it in a do while since the choice won't be initialized yet:

do
{
	cout << "Error. Please enter either male or female for your characters gender." << endl;
	getline ( cin, enteredCommand);
}while ((enteredCommand != "male") && (enteredCommand != "female"));





edited for typos

This post has been edited by KYA: 01 May 2009 - 11:31 AM

Was This Post Helpful? 0
  • +
  • -

#3 OsuSensei  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 01-February 09

Re: Multiple Strings in a While Statement

Posted 01 May 2009 - 11:36 AM

Cool it works now. Thanks a lot KYA. :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1