Help with a simple text-based game?

So I have been stuck on this part for a while...
I have been trying to make gender for my character so i could use he or she depending on gender, but cant seem to find the right way to do it.
The way I have it right now is sufficient but it is certainly not the best, if someone could explain or change my code so that when a user entered their gender it would associate that with "he" (for male) or "she" (for female) I would be very thankful... please respond.

#include <iostream>
#include <string>

using namespace std;

int main()
{
cout << "Welcome, please enter your player's name here." << endl;

string playerName;
cin >> playerName;

cout << "Oh, so you are " << playerName << ".\nIf so click 'y' if not click 'n'" <<endl;

char choice;
cin >> choice;

if (choice=='y')
{
cout << "Well then we are ready to begin our journey." << endl;

}

if (choice=='n')
{
cout << "Then what is your name?" << endl;
cin >> playerName;
}

cout << "Is your player male (enter 'he') or female (enter 'she')?" << endl;
string gender;
cin >> gender;
cout << playerName << " is walking through the woods when " << gender << " stumbles across a fork in the trail." << endl;


return 0;
}
You could use an if statement to check to see if the user inputted a he or she.
Last edited on
Friendly Tip:

Please use code tags when you post. Makes it easier for everyone to view your code, as well as help you solve your problem.
Also you should do some input validation. What if I typed "lolgenderwut?" where you expected a he or she?
well u can accept the choice into an integer variable g: 0 for male and 1 for female, and then assign the value "He" or "she" accordingly into string.............Hope this helps.......... :)
From what ResidentBiscuit has said. Maybe you should use a do-while loop for input.
Also, what mj1709 said, maybe consider taking away the ability of the user changing the string:

1
2
3
4
5
6
7
8
9
10
11
bool isMale = true;

do
{
    //get cin here
}while( /*validation check here*/ )

if( isMale )
    string = "he";
else
    //... 
Last edited on
Topic archived. No new replies allowed.