Having Class Probs

a
Last edited on
1. Just have setPlayerName also return the name:
1
2
3
4
5
6
7
8
9
10
11
12
string Player::setPlayerName()
{
	cout << "Please Enter Your name" << endl;
	cin >> A;
        return A;
}

int main()
{
 Player me;
 string name = me.setPlayerName();
}


Although there's really no point in storing it twice. You could just use a getter function:
1
2
3
4
string getPlayerName()
{
return A;
}


2. Use a loop + switch:
1
2
3
4
5
6
7
8
9
10
11
12
while(quit == false)
{
	std::cin>>choice;
	switch(choice)
	{
	case Q: quit = true; break; //Q for quit
	case P: playGame(); break; //P for play
	case I: printInstructions(); break; //I for instructions
	}
}
//You'll obviously need to write playGame() and whatever else you want to implement
//but something like this will also make your code more modular which is a good thing. 


Note: A is a terrible name for Name. Just call it "Name".
When I try to return A;

I come up with the following error

1 IntelliSense: return value type does not match the function type



Make sure you declare the function with a return value of string in the "Player" declaration as well:
1
2
3
4
5
6
7
8
9
10
class Player
{
std::string setPlayerName();
};
//And then the definition:
std::string Player::setPlayerName()
{
//Code
return A;
}
Topic archived. No new replies allowed.