Help naming something?

I've been starting to put all my variables in private, and since i've been doing that I have no idea how to do anything anymore. Before if I wanted to name something I could just do something like string name; then cin >> name;
I've been trying to figure out how to do this while making them private and can't make it work. I still don't even know what im suppost to have in my class and what not to have in it.

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
43
44
45
46
47
48
49
50
51
 #include <iostream>
#include <string>
using namespace std;



class Q{

   public:
        void setsword(string x){
            Sword = x;
        }


    string getsword(){
        return Sword; }





   private:
   string Sword;
        };

int main(){

    char response;


    cout << "You are walking through a forest and discover clearing with a giant rock in the middle. You go up to the rock and discover a sword stuck in the middle of it." << endl;
    cout << "Do you want to attempt to pull the sword out? y/n" << endl;
    cin >> response;
    if (response == 'y'){
            cout << "You walk up to the sword and attempt to pull it out with all your strength, but it doesn't seem to come out." << endl;
            cout << "Keep trying? y/n";
    }
            cin >> response;
    if (response == 'y'){
            cout << "SUCCESS!!! You manage to pull the sword out, What would you like to call your new sword?" << endl;
            Q inwin;
            cin >> inwin.setsword(); //right here is what i'm having problems with

    }




    return 0;
}
Last edited on
You need to do the cin to a string, then pass the string to setsword().

1
2
3
    string name;
    cin >> name;
    inwin.setsword(name); 


You'd have to create an intermediate variable to hold the sword name first, since you cannot access Q::Sword directly.

1
2
3
4
5
Q inwin;
string temp;
cout << "enter sword name: ";
cin >> temp;
inwin.setsword(temp);
Topic archived. No new replies allowed.