I'm simply trying to store player data in its own class, and use a function to display the data in the class.
I have all my player data stored inside the Player
class, I have no problem outputting the private variables into main but I made a function that will grab all the player data and display it.
The getPInfo
function inside the Player
class should grab the name from the class and put it into the pName
string inside the getPlayer
function and display it. But as you can imagine, it doesn't. The getPInfo function doesn't succeed in passing names value to the getPlayer function.
Keep in mind I'm no C++ expert, so please make your answer approachable.
#include "stdafx.h"
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::endl;
using std::string;
class Player {
public:
Player() {
name = "?";
}
void getPInfo(string x) {
x = name;
}
void setName(string x) {
name = x;
}
private:
string name;
};
void getPlayer() {
string pName;
Player player;
player.getPInfo(pName);
cout << "Name = " << pName << endl;
}
int main()
{
string str;
cout << "What is your name?\n" << endl;
cin >> str;
Player pObj;
pObj.setName(str);
getPlayer();
}