Cannot figure out how to let user input name that is called by a class

I am trying to let the user choose the name for his/her pokemon. What am I doing wrong?

Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include "Monsters.h"

using namespace std;

int main()
{
    string playerchoice;
    Monsters pikachu;

    cout << "What do you want to name your Pokemon? ";
    cin >> playerchoice;
    playerchoice = pikachu.SetName();
    
    cout << "Your Pikachu, " << pikachu.GetName() << ", has a health of " << pikachu.GetHealth() << " and an attack of " << pikachu.GetAttack();


    return 0;
}



Monster.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#ifndef MONSTERS_H
#define MONSTERS_H
#include <iostream>
#include <string>

using namespace std;

class Monsters
{
    public:
        Monsters();
        int GetHealth();
        void SetHealth(int);
        int GetAttack();
        void SetAttack(int);
        string GetName();
        void SetName(string);

    private:
        string name;
        int health;
        int attack;
};

#endif // MONSTERS_H



Monster.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

#include "Monsters.h"
#include <iostream>
#include <string>

using namespace std;

Monsters::Monsters()
{
    health = 100;
    attack = 5;
}

int Monsters::GetHealth()
{
    return health;
}

void Monsters::SetHealth(int TheHealth)
{
    TheHealth = health;
}

int Monsters::GetAttack()
{
    return attack;
}
void Monsters::SetAttack(int TheAttack)
{
    TheAttack = attack;
}

string Monsters::GetName()
{
    return name;
}
void Monsters::SetName(string TheName)
{
    TheName = name;
}
void SetName(string); is expecting to see a string but u don't pass anything into it on line 13. Just pass 'playerchoice' into the function - there won't be an equal sign on line 13.

Also I noticed that
1
2
3
4
void Monsters::SetName(string TheName)
{
    TheName = name;   //this is backwards - u want to store the TheName into 'name'
}
same go for the rest of theSet functions .
They are all backwards.
You want to store whatever that is being stored in the parameter to the private variable
Topic archived. No new replies allowed.