Help with while loops skipping cin input.

So I am working on a little project to help me strengthen my coding skills and I am having some issues with while loops. Whenever my program gets to this snippet of code it goes crazy.

How can I make this run better?

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
    if(playerrow == slime0row && playercol == slime0col){
        while(slime0dead == false){
            cout << "Slime encountered! What will you do?" << endl;
            cout << " W-Attack!   S-Defend   A-Escape   F-Use Item" << endl;
            cout << ">>";
                cin >> slime0encounter;
                switch(slime0encounter){
                    case 'W':
                        slime0HP - playerATT;
                        cout << "You hit the slime for " << playerATT << " damage." << endl;
                        break;
                    case 'S':
                        break;
                    case 'A':
                        break;
                    case 'F':
                        break;
                    default:;



                }

        }
    }
you probably have a cin before the while loop.

What happens is that when you enter the input and press ENTER for the first input, cin , takes the input and leaves '\n' in the input buffer.

It is this '\n' that is inputted in the next call to cin .
To fix this, after the first call to cin , add the line cin.ignore() to ignore the next character which is '\n' .
I'm a little confused with how to do this.

I added the rest of my code so far, hope this helps.

EDIT: the code I'm having the issue with starts on line 132

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
#include <iostream>
#include <stdlib.h>

using namespace std;

const int ROWS = 11;
const int COLS = 11;
      int currentrow   = 0;
      int currentcol   = 0;

      int playerrow    = 3;
      int playercol    = 3;

      int slime0row    = 9;
      int slime0col    = 9;
      int slime0action = 0;
      int slime0encounter;
      int slime0HP     = 30;
      int slime0ATT    = 4;
const int slime0EXPgive= 3;
      bool slime0dead = false;

      int slime1row    = 8;
      int slime1col    = 8;
      int slime1action = 0;
      bool slime1dead = false;

      int slime2row    = 7;
      int slime2col    = 7;
      int slime2action = 0;
      bool slime2dead = false;

      int playerHP     = 100;
      int playerATTbuff= 0;
      int playerLVLATT = 0;
      int playerATT    = 6 + playerATTbuff + playerLVLATT;
      int playerEXP    = 0;
      int playerLVL    = 0;
      bool playerdead = false;




void mapping(int currentrow, int currentcol, int playerrow, int playercol){
    while(currentrow <= ROWS){

        while(currentcol <= COLS){
            if(playerrow == currentrow && playercol == currentcol) cout << " P ";
            else if(slime0row == currentrow && slime0col == currentcol && slime0dead == false) cout << " S ";
            else if(slime1row == currentrow && slime1col == currentcol && slime1dead == false) cout << " S ";
            else if(slime2row == currentrow && slime2col == currentcol && slime2dead == false) cout << " S ";
            else cout << " - ";
            ++currentcol;
        }
        cout << endl;
        currentcol = 0;
        ++currentrow;
    }
}

int main()
{
    int loop0 = 1;
    while(loop0 >= 1){
        cout << "Little rpg" << endl;
        cout << "By Tannan Dudley" << endl;
        loop0 = 0;
    }
   while(playerdead == false){
    //Level up check
    if(playerEXP >= 20){
        playerLVL = playerLVL + 1;
        playerHP = 100;
        playerLVLATT + 2;
        playerEXP = 0;
        cout << "Level Up!" << endl;
    }
      cout << "****************************" << endl;
      cout << "* HP: " << playerHP << "/100  EXP: " << playerEXP << "/20   *" << endl;
      cout << "****************************" << endl;
      cout << endl << endl;
    //Movment of three slimes
    if(slime0dead == false){
        slime0action++;
            while(slime0action >= 2){
                int slime0moveroll0 = rand() % 100;
                int slime0moveroll1 = rand() % 100;
                if(slime0moveroll0 >= 50) slime0row++;
                    else slime0row--;
                if(slime0moveroll1 >= 50) slime0col++;
                    else slime0col--;
                if(slime0row > ROWS)       slime0row--;
                if(slime0row < ROWS - ROWS)slime0row++;
                if(slime0col > COLS)       slime0col--;
                if(slime0col < COLS - COLS)slime0col++;
                slime0action = 0;
            }
    }
    if(slime1dead == false){
        slime1action++;
            while(slime1action >= 2){
                int slime1moveroll0 = rand() % 100;
                int slime1moveroll1 = rand() % 100;
                if(slime1moveroll0 >= 50) slime1row++;
                    else slime1row--;
                if(slime1moveroll1 >= 50) slime1col++;
                    else slime1col--;
                if(slime1row > ROWS)       slime1row--;
                if(slime1row < ROWS - ROWS)slime1row++;
                if(slime1col > COLS)       slime1col--;
                if(slime1col < COLS - COLS)slime1col++;
                slime1action = 0;
            }
    }
    if(slime2dead == false){
        slime2action++;
            while(slime2action >= 2){
                int slime2moveroll0 = rand() % 100;
                int slime2moveroll1 = rand() % 100;
                if(slime2moveroll0 >= 50) slime2row++;
                    else slime2row--;
                if(slime2moveroll1 >= 50) slime2col++;
                    else slime2col--;
                if(slime2row > ROWS)       slime2row--;
                if(slime2row < ROWS - ROWS)slime2row++;
                if(slime2col > COLS)       slime2col--;
                if(slime2col < COLS - COLS)slime2col++;
                slime2action = 0;
            }
    }
        // FIGHT
    if(playerrow == slime0row && playercol == slime0col){
        while(slime0dead == false){
            cout << "Slime encountered! What will you do?" << endl;
            cout << " W-Attack!   S-Defend   A-Escape   F-Use Item" << endl;
            cout << ">>";
                cin >> slime0encounter;
                cin.ignore();
                switch(slime0encounter){
                    case 'W':
                        slime0HP - playerATT;
                        cout << "You hit the slime for " << playerATT << " damage." << endl;
                        break;
                    case 'S':
                        break;
                    case 'A':
                        break;
                    case 'F':
                        break;
                    default:;



                }

        }
    }

    mapping (currentrow, currentcol, playerrow, playercol);
    cout << "W-up   S-down    A-left    D-right" << endl;
        char playermove;
        cout << ">>";
        cin >> playermove;
            switch(playermove){
                case 'W':
                    playerrow--;
                        if(playerrow < ROWS - ROWS){
                            cout << "Out of map area." << endl;
                            playerrow++;
                        }
                        break;
                case 'S':
                    playerrow++;
                        if(playerrow > ROWS){
                            cout << "Out of map area." << endl;
                            playerrow--;
                        }
                        break;
                case 'A':
                    playercol--;
                        if(playercol < COLS - COLS){
                            cout << "Out of map area." << endl;
                            playercol++;
                        }
                        break;
                case 'D':
                    playercol++;
                        if(playercol > COLS){
                            cout << "Out of map area." << endl;
                            playercol--;
                        }
                        break;

                default:
                    ;
            }
   }
    return 0;
}


Last edited on
Topic archived. No new replies allowed.