Problem using classes (help please)

How can I let the user input string threw a public class function into the private class variable called name?
I tried this, and it doesn't work.

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
41
42
#include <iostream>
#include <string>

using namespace std;

class users
{
  public:
         users()
         {
               health = 100 * lvl;
               lvl = 1;
               str = 0 + lvl;
                       
         }
         void setname(string x)
         {
                name = x;   
         }
         string getname()
         {
                return name;       
         }      
  private:
          string name;
          int health;
          int lvl;
          int str;
          
      
};

int main()
{
    cout << "Type your name: ";
    users player;
    cin >> player.setname();
    cout << "\nWelcome to the txt game " << player.getname() << endl;
    
    system("pause>null");
    return 0;   
}


The return type of function setname is void so you are trying to enter a value to void.

cin >> player.setname();

You should define an object of type string, enter its value, and then pass that string as an argument to function setname
oh.... ok, thanks! :)
Topic archived. No new replies allowed.