0

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();
}
StoryTeller - Unslander Monica
  • 165,132
  • 21
  • 377
  • 458
More Umph
  • 13
  • 4
  • either `void getPInfo(string& x)` or even better, returning the string as already suggested in the answer. – JFMR Sep 20 '17 at 07:33
  • I tried using a return and updated the code, can't find an example of how to use return this way though. – More Umph Sep 20 '17 at 07:41
  • `string setName() { return name; }` what did you try? – JFMR Sep 20 '17 at 07:45
  • 1
    please dont update the question with fixes according to answers you got. Currently the question and the answers are completely confusing, because the question talks about behaviour that is not present in the code and the answers suggest fixes for problems that are also not present in the code – 463035818_is_not_an_ai Sep 20 '17 at 08:26
  • 1
    @tobi303 - Didn't even notice that when fixing up the OP's text. Edited back to original code sample. – StoryTeller - Unslander Monica Sep 20 '17 at 08:35

3 Answers3

1

fixed it and made it somewhat more c++ish:

    #include <iostream>
    #include <string>
    using std::cout;
    using std::cin;
    using std::endl;
    using std::string;


    class Player {
    public:
        Player()
            : name("?")
        {}

        const string& getPInfo() const{
            return name;
        }
        void setName(const string& x) {
            name = x;
        }

    private:
        string name;
    };


    void getPlayer(const Player& player) {
        string pName;
        pName = player.getPInfo();

        cout << "Name = " << pName << endl;
    }

    int main()
    {
        string str;

        cout << "What is your name?\n" << endl;
        cin >> str;
        Player pObj;
        pObj.setName(str);
        getPlayer(pObj);
    }
kiloalphaindia
  • 561
  • 3
  • 7
  • I dont understand how/why the cost and & everywhere, or how in main getPlayer(pObj) just takes an object as a parameter. – More Umph Sep 20 '17 at 08:17
  • c++ supports const correctness. By using const and & (reference) you are passing a reference to an object, that is not supposed to be changed. This avoids copying the object on each function call, which happens when you pass an object by value. The const after a member function makes the this pointer const allowing the function to be called on a constant object. – kiloalphaindia Sep 20 '17 at 08:25
  • Thanks dude with what you posted I figured out what I need to learn to make this program a possibility – More Umph Sep 21 '17 at 01:36
0

The problem presumably being getPIinfo() ?

You want to check your book how return works.

MSalters
  • 173,980
  • 10
  • 155
  • 350
0

Well, problem number one is argument x in Player::getPInfo(std::string) is not used. As suggested, you should just use the return value and get rid of the argument.

std::string getPInfo() const { return this->name; }

This means your getPlayer() function will need to change some. Since you aren't actually getting a player with this function perhaps we could rename it to displayPlayer and take a Player as an argument.

void displayPlayer(Player const& player) { 
    std::cout << "Name = " << player.getPInfo() << std::endl;
}

Once this is done you can update the last line of your main to display the Player it created.

displayPlayer(pObj);
ebclark
  • 45
  • 4
  • I updated the code, error at line 30. Why the const& though? And how does displayPlayer take pObj as a parameter? – More Umph Sep 20 '17 at 08:19
  • I don't know what's on line 30, or what error it produced. By chance did you miss adding const to getPInfo()? I don't understand your last question regarding how displayPlayer takes pObj as a parameter. `Player const& player` and `const Player & player` are equivalent and I just typed the former by habit. – ebclark Sep 20 '17 at 10:27