loop/getline runtime error

hi im trying to get this loop working. the user inputs what they would like to do and then an if statement compares the variable. but when i run it, it print the first cout statement in the loop two times in the beginnings or after 2 inputs it doesnt work. you might have to rename the variables because i have a big header file with classes and member data.

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
#include <iostream>
#include <string>
#include <vector>
#include <ctime>
#include <cstdlib>
#include "Data.h"

using namespace std;

int main()
{
    //player variables
    Warrior Player;
    Player.ArmorValue = 2;
    Player.Level = 1;
    Player.Experience = 0;
    Player.Gold = 0;
    int CritChance;

    Rat ratNPC;
    ratNPC.Health = 30;
    ratNPC.Strength = 5;
    ratNPC.Agility = 5;
    ratNPC.ArmorValue = 0;

    Bandit banditNPC;

    Giant giantNPC;

    Dragon dragonNPC;

    string Choice;


    //Player.StatsCalc(Player.Agility, Player.Health, Player.Strength);
    //CritChance = Player.Agility / 2;

    while (true)
    {
        cout << "What are you going to do? check stats, fight, or exit?" << endl;
        cin.get();
        getline(cin, Choice);
        



        if (Choice == "exit")
        {
            return 0;
        }

        if (Choice == "check stats")
        {
            cout << "You do " << Player.Strength << " damage" << endl;
            cout << "You have" << Player.Health << " health" << endl;
            cout << "your critical strike chance is   " << CritChance << " %" << endl;
            cout << "You have " << Player.Gold << " gold" << endl;
            cout << "Your level " << Player.Strength << endl;
            cout << "You have " << Player.Experience << " experience" << endl;
        }

          }
}
Last edited on
Remove this line:

cin.get();
i was using that to stop the lines from coming up too much, but its not working either way. getting rid of cin.get() kind of helps but why does it put up 2 of the same lines right away?
Last edited on
I don't see the issue. It doesn't repeat the same line twice for me.
try this after line 40:

while (cin.get() != '\n') continue;
Topic archived. No new replies allowed.