Cin nothing for nothing?

Hello forum, i have an mini snake in development, but i see at line 19-21, when i give the "ch" value, after pressing enter it requer to press another enter to start the "while", i need to skip that last enter from keyboard, how to do it? Im beginner.

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

using namespace std;

short int x;
short int y;
string ch;

int main()
{
    char kp;
    int av;

    cout<<"Enter your character: ";
    cin >> ch;
                             //why 1 more pressing enter from keyboard?, to activate the 'while'??
    while(1)
    {
        kp=getch();
        av=kp;

        /////////////////////////////////////////////////////

        if(av==75)     //keys
        {
            x = x - 1;
        }
        if(av==77)
        {
            x = x + 1;
        }
        if(av==72)
        {
            y = y - 1; //Inversed for user's vision
        }
        if(av==80)
        {
            y = y + 1; //Inversed for user's vision
        }

        ///////////////////////////////////////////////////////

        if(x==0)    //Protection for border //begin
        {
            x = 1;
        }
        if(y==0)
        {
            y = 1;
        }

        if(x==19)    //Protection for border //end
        {
            x = 18;
        }
        if(y==10)
        {
            y = 9;
        }

        ////////////////////////////////////////////////////////

        system("cls");

        COORD coord = {x, y};
        SetConsoleCursorPosition (GetStdHandle (STD_OUTPUT_HANDLE), coord);
        cout<<ch;
    }
    return 0;
}
You're asking for two inputs. One at line 19, the other at line 23. So two enters are expected.
And how to skip that? If you run the codes in codeblocks you will see the problem.
You use ch at line 71 and you use kp at line 24. If you skip one of the two inputs, one of those values will be uninitialized.
Topic archived. No new replies allowed.