Having trouble with getline

I'm trying to program a really simple RPG with my friend from work based on this game we play there leveling him up. In my "main.cpp" I have the person enter their name and the std::getline handles it fine and outputs it correctly. However, when my program get's to the line where it asks for your inventory item, it doesn't give me an option to enter it and it just goes directly to the "calculating current stats" output. I don't understand what I'm doing wrong because I set it up the exact way as when the user enters their name earlier and that one works correctly. Any help would be greatly appreciated thanks!

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#include <iostream>

#include "SIMON.HPP"

int main () {

    simon s1;    
    std::string name;
    double level;
    double hitpoints;
    double strength;
    double defense;
    double vitality;
    double abilitypoints;
    std::string inven;

    std::cout << "Enter your name:" << std::endl;
    std::getline(std::cin, name);
    std::cout << "Enter your level:" << std::endl;
    std::cin >> level;
    std::cout << "Enter your HP:" << std::endl;
    std::cin >> hitpoints;
    std::cout << "Enter your strength:" << std::endl;
    std::cin >> strength;
    std::cout << "Enter your defense:" << std::endl;
    std::cin >> defense;
    std::cout << "Enter your vitality:" << std::endl;
    std::cin >> vitality;
    std::cout << "Enter your AP:" << std::endl;
    std::cin >> abilitypoints;
    std::cout << "Enter your inventory item:" << std::endl;
    std::getline(std::cin, inven);

    std::cout << "Calculating Current Stats ..." << std::endl;
    std::cout << "---------------------------" << std::endl;
    std::cout << "-----------%%--------------" << std::endl;
    std::cout << "-----------%%--------------" << std::endl;
    std::cout << "---------------------------" << std::endl;

    s1.setName(name);
    s1.setLvl(level);
    s1.setHp(hitpoints);
    s1.setStr(strength);
    s1.setDef(defense);
    s1.setVit(vitality);
    s1.setAp(abilitypoints);
    s1.setinItem(inven, 5);
    s1.setWeapon("Olive Sword", 15);

    s1.displayStats();
    
    return 0;
};


#include "SIMON.HPP"


void simon::setName(std::string n) {
    name = n;    
}

void simon::setLvl(double l){
    lvl = l;
}

void simon::setHp(double h){
    hp = h;
}

void simon::setStr(double s){
    str = s;
}

void simon::setDef(double d){
    def = d;
}

void simon::setVit(double v){
    vit = v;
}

void simon::setAp(double a){
    ap = a;
}

void simon::setinItem(std::string s, int i){
    inItem = s;
    inb = i;
}

void simon::setWeapon(std::string w, int i){
    weapon = w;
    wnb = i;
}

void simon::displayStats(){


    std::cout << "                      " << std::endl;
    std::cout << "                      " << std::endl;
    std::cout << "-------||+~~+||-------" << std::endl;
    std::cout << name << "'s current stats:" << std::endl;
    std::cout << "---------****---------" << std::endl;
    std::cout << "     LVL: " << getLvl()  << std::endl;
    std::cout << "     HP:  " << getHp()  << std::endl;
    std::cout << "     STR: " << getStr() << std::endl;
    std::cout << "     DEF: " << getDef() << std::endl;
    std::cout << "     VIT: " << getVit() << std::endl;
    std::cout << "     AP:  " << getAp() << std::endl;
    std::cout << "     DMG: " << getDmg() << std::endl;
    std::cout << "     WPN: " << getWeapon() << std::endl;
    std::cout << "     INV: " << inItem << std::endl;
    std::cout << "---------****---------" << std::endl;
    std::cout << "     T.W.A.O.K.K.     " << std::endl;
    std::cout << "-------||+~~+||-------" << std::endl;
    std::cout << "                      " << std::endl;
    std::cout << "                      " << std::endl;
}


double simon::getLvl(){
    return lvl;
}

double simon::getHp(){
    return hp;
}

double simon::getStr(){
    return str;
}

double simon::getDef(){
    return (vit / 2) + def + inb;
}

double simon::getVit(){
    return vit;
}

double simon::getAp(){
    return ap;
}

double simon::getDmg(){
    return (str / 2) + wnb;
}

std::string simon::getinItem(){
    return inItem;
}

std::string simon::getWeapon(){
    return weapon;
}



#include <iostream>
#include <string>

class simon{

    public:
        
        void setName(std::string);
        void setLvl(double);
        void setHp(double);
        void setStr(double);
        void setDef(double);
        void setVit(double);
        void setAp(double);
        void setinItem(std::string, int);
        void setWeapon(std::string, int);
        void displayStats();

        double getLvl();
        double getHp();
        double getStr();
        double getDef();
        double getVit();
        double getAp();
        double getDmg();
        
        std::string getinItem();
        std::string getWeapon();



    private:

        double lvl;
        double hp;
        double str;
        double def;
        double vit;
        double ap;

        double dmg;
        double inb;
        double wnb;
        
        std::string name;
        std::string inItem;
        std::string weapon;

};
There does not seem to be much wrong with your code, so I think the program is running through to completion and closing.

To pause the program before it dies add this...

1
2
std::cin.ignore();
std::cin.get();


Put that just above your return 0;
I tried what you said and it's still doing the same thing. Thank your for trying to help though!
ok sorry. Add this line...
1
2
3
std::cout << "Enter your inventory item:" << std::endl;
	std::cin.ignore(); //put this here.
	std::getline(std::cin, inven);
Hello Symphoneee ,

A "std::getline()" followed by "std::cin >>" works just fine, but "std::cin >>" followed by a "std::getline()" does not work well because the "std::cin >>" will leave the new line character in the input buffer that the getline will extract and move on with out waiting for any input from the user. As Mange showed you this is a way to clear up the problem, but I would suggest std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>. as std::cin.ignore(); may only ignore one character and this may not be enough. Although in some cases it does work.

Any "std::getline()" that is preceded by a "std::cin >>" needs the use of "std::cin.ignore(...)" to clear the input buffer to work properly.

Hope that helps,

Andy
In general, I think std::cin.ignore() belongs after std::cin>> rather than before std::getline().

The reason I think so is because it's not always easy to know if you need to use std::cin.ignore() before std::getline(). It depends on what input operation was used previously. If you use std::getline() at the beginning of a function, or after a loop that use std::cin>>, you can easily end up in situations where std::cin.ignore() is sometimes the right thing to do and sometimes it isn't but you have no way of telling.

If you say that any piece of code that reads input is responsible for reading the whole line it becomes much easier to write correct code. You no longer need to worry about doing anything special before using std::getline(), but it does mean you will have to use std::cin.ignore() after std::cin>>. If you use std::cin>> many times in a row it's probably a bit overkill to use std::cin.ignore() each time, but at least use it at the end of functions and in other situations where you are not sure what the next input operation is going to be.
Last edited on
Well in this case the use of std::cin.ignore() works fine because the last entry was a double value and not a string. So there is not a lot of junk needing to be cleared from the buffer.

@Peter87, you make a good point so for better readability your placement is correct.

@Symphoneee, we have decided this is how it should go...

1
2
3
std::cin >> abilitypoints;
std::cin.ignore(); //place code here.
std::cout << "Enter your inventory item:" << std::endl;
Last edited on
Hey thanks a lot everyone! I think I understand it now. The explanations were great.
Topic archived. No new replies allowed.