kbhit() and getch()

Pages: 12
how can i use both kbhit() and getch() for a value entered by the user?
ie i check if user inputs a value and then use the same value in another condition(if).
Last edited on
These functions are sort of "illegal" and are not used anymore in standard C++.
Anyway, using getch(); with GetAsyncKeyState();--windows--:

1
2
3
4
5
char strInput;

cout<<"Hit enter key";
strInput=getch();
If(GetAsyncKeyState()==VK_RETURN) cout<<"Good";


Maybe you can replace GrtAsyncKeyState(); with kbhit(); I don't know cause I've not used it before
I am actually making a game where there is a loop continuously running which shouldn't be stopped and a statement that if something is entered, check its value and return a specific output.
closed account (NwvkoG1T)
see kbhit() just checks if there is something in the input buffer. it does not do anything else.

instead you could simply use getch() which will take the charachter if its there and if not it will force the user to enter a charachter. that is the safest and most easy way to do what you are tryin to achieve.
but that would stop a running loop and wait till the user inputs a value. bt i want the loop to continue running even if the user doesnt input anything

i am actually trying to make a easier football game(2 player)
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
#include<iostream.h>
#include<conio.h>
#include<dos.h>
void main()
{clrscr();
int y=3,t=4,r=6,h=3,g=4,f=6; char a,s;
for(int i=0;i<=125;i++)
{gotoxy(1,1);
cout<<"  ------------------------------";
gotoxy(1,11);
cout<<"  ------------------------------";
gotoxy(3,2);cout<<"|";
gotoxy(3,3);cout<<"|";
gotoxy(3,4);cout<<"|";
gotoxy(3,5);cout<<"|";
gotoxy(3,6);cout<<"|";
gotoxy(3,7);cout<<"|";
gotoxy(3,8);cout<<"|";
gotoxy(3,9);cout<<"|";
gotoxy(3,10);cout<<"|";
gotoxy(32,2);cout<<"|";
gotoxy(32,3);cout<<"|";
gotoxy(32,4);cout<<"|";
gotoxy(32,5);cout<<"|";
gotoxy(32,6);cout<<"|";
gotoxy(32,7);cout<<"|";
gotoxy(32,8);cout<<"|";
gotoxy(32,9);cout<<"|";
gotoxy(32,10);cout<<"|";
a=getch();
clrscr();
if(a=='2'&&y<4) y++;
else if(a=='8'&&y>2) y--;
gotoxy(20,y)  ; cout<<"0";
gotoxy(20,y+1); cout<<"|";
gotoxy(20,y+2); cout<<"0";
gotoxy(20,y+3); cout<<"|";
gotoxy(20,y+4); cout<<"0";
gotoxy(20,y+5); cout<<"|";
gotoxy(20,y+6); cout<<"0";
if(a=='2'&&t<6) t++;
else if(a=='8'&&t>2) t--;
gotoxy(10,t)  ; cout<<"0";
gotoxy(10,t+1); cout<<"|";
gotoxy(10,t+2); cout<<"0";
gotoxy(10,t+3); cout<<"|";
gotoxy(10,t+4); cout<<"0";
if(a=='2'&&r<10) r++;
else if(a=='8'&&r>2) r--;
gotoxy(5,r)  ; cout<<"0";

if(a=='s'&&h<4) h++;
else if(a=='w'&&h>2) h--;
gotoxy(15,h)  ; cout<<"X";
gotoxy(15,h+1); cout<<"|";
gotoxy(15,h+2); cout<<"X";
gotoxy(15,h+3); cout<<"|";
gotoxy(15,h+4); cout<<"X";
gotoxy(15,h+5); cout<<"|";
gotoxy(15,h+6); cout<<"X";
if(a=='s'&&g<6) g++;
else if(a=='w'&&g>2) g--;
gotoxy(25,g)  ; cout<<"X";
gotoxy(25,g+1); cout<<"|";
gotoxy(25,g+2); cout<<"X";
gotoxy(25,g+3); cout<<"|";
gotoxy(25,g+4); cout<<"X";
if(a=='s'&&f<10) f++;
else if(a=='w'&&f>2) f--;
gotoxy(30,f)  ; cout<<"X";



}
getch();
}


(im a young and bad programmer)the code above is for the players to move
i want to incert a ball which will continuosly move left or right which would need a loop to keep running and the above program is executed only if a value is input.
If you want the program to continue running without waiting for a keypress, test kbhit before getch
1
2
3
4
5
if (kbhit())
{
    ch = getch();
    // etc...
}


Example:
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
#include <iostream>
#include <windows.h>
#include <conio.h>

    using namespace std;

int main()
{
    bool repeat = true;
    while (repeat)
    {
        cout << '*';
        Sleep(100);

        if (kbhit())
        {
            char ch = getch();
            switch (ch)
            {
                case 27:               // press ESC to exit
                    repeat = false;
                    break;
                case 32:               // press SPACE to clear screen
                    clrscr();
                    break;
            }
        }
    }
    return 0;
}
Last edited on
closed account (NwvkoG1T)
But in the reply by chervil the user will have to enter the key twice as getch() takes the charachter from the keyboard and not from the input buffer.

But the following code should work:

if(kbhit)
ch=cin.get(ch);

The get function takes the code from the input buffer.
im new to c++ can u plz giv me an example code for the above because after i tried the code it showd me"cant convert istream to char"
closed account (NwvkoG1T)
okay fine!
see suppose in ur game u have for moving a player forward
an u use the function forward() to achieve this.

so simply do this:

if(kbhit())
ch=cin.get(ch);

if(ch=='f') forward();

got it now? i think this should work.
getch() takes the charachter from the keyboard and not from the input buffer
I'm not sure about that, perhaps it is implementation-dependent, since conio.h is a non-standard library. On my system it isn't a problem, the buffer is empty after using getch().

This code is not valid:
1
2
    char ch;
    ch=cin.get(ch);
because the return type of cin.get() is iostream, and cannot be assigned to a char.

The compiler error can be removed by changing the code like this.
1
2
    char ch;
    cin.get(ch);

However, there is still the problem that cin.get() will wait for the user to press enter, thus it does not serve the same or similar purpose as the code I originally proposed.

You should be able to use istream.peek() to read the next value in the buffer, then extract the character with cin.get() or the extraction operator if the character is present.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
char pk = 0;
char ch;
pk = cin.peek();
/* Test the input stream state, peek() can set an error condition
    that will need to be cleared and reset. */
if(!cin)
{
   cin.clear();
   // cin.ignore(100,'\n'); // You may also want to remove anything in the buffer.
}
else
{
   if(pk > 0)
      ch = cin.get(); // or cin.get(ch) // or cin >> ch;
}


See this link for more information on peek: http://www.cplusplus.com/reference/istream/istream/peek/

See this link for more information on istream.get: http://www.cplusplus.com/reference/istream/istream/get/



Last edited on
cin.peek() is blocking.
closed account (NwvkoG1T)
Sir. What chervil says is correct. Of course it won't be ch=cin.get(ch) but simply cin.get(ch).

And I don't know about the non standardisation of conio.h. It is better if u depend on this rather than the changing definition of conio.

Also I am quite sure that cin.get simply takes the charachter from the input stream. Not from keyboard. So if there is something in the input stream (we are checking) then definitely it would be taken into ch.
like icyflame told


1
2
if(kbhit)
cin.get(ch);


i am experiencing a problem that before i type anything, the program has passes the code and i am not able to type anything.
if (kbhit())
will be true after you have already typed something.

In a game loop it makes sense, the condition is false when nothing has been typed, so the loop continues. But when a key was pressed, it will be caught on the next pass through the loop.
Last edited on
closed account (NwvkoG1T)
Yes. What Chervil says makes complete sense.

Actually i think what you are experiencing can be corrected by clearing the buffer before you start the game. I think that is the problem. As you are not clearing the buffer before the game, the charachter that you entered last is getting stored in ch.
Assuming you are using Turbo C as the compiler
Use this code in the beginning of the code to clear the buffer
while(kbhit()) getch(); This wont run an infinite loop.
now use this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
for() // whatever your loop is
{
      //move the ball here
      if(kbhit())
      {
               char ch = getch();
               switch(ch)
               {
                                //do what you want with the loop
               }
               while(kbhit()) getch();  //clear the buffer again although it is not necessary, remove this to make the game smoother
       }
       //add a wait here if you want
}

And this code works perfectly and just forget cin.get() or any other complex command like cin.peek or so.
If you are trying to use arrow keys just reply, you will have to extend your code for that.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
       #include<iostream.h>
       #include<conio.h>
       void main()
       {char ch;
       for(int i=0;i<=10;i--)
       {if(kbhit())
       {ch=getch();
       switch(ch)
       {case 'f':
       cout<<'f';}}}
       getch();
       }

as soon as i enter anything the program finishes.i dont know what statement is causing this.
it just goes to kbhit condition den to the last getch();
Last edited on
The code i gave you dosen't wait for you to press the key, and as the interval of i = 1 to 10 is so small that till you press any key i becomes 11.
See this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<iostream.h>
#include<conio.h>
void main()
{
    for(int flag = 1; flag == 1; /*Nothing here*/ ) // This code will go on till someone change the value of flag
    {
         // do something here like moving the ball  
        if(kbhit())  // If you have pressed any key
        {
            char ch = getch();
            switch(ch)
            {
                case 'f': cout<<"f"; break;
                case 27: flag = 0;  //27 for the ESC key, the program will terminate when someone press the ESC key
            }
        }
    }    
}


Now in this code whenever you press f it will print it out or whenever you press ESC the program will terminate.
If you want the user to wait for the keypress just use getch() without kbhit().
Last edited on
plz help me improve on my game
thanks to all!
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
#include<iostream.h>
#include<conio.h>
#include<dos.h>
void display(int y,int t,int r,int h,int g,int f,int i,int j,int sc1,int sc2)
{clrscr();
gotoxy(1,1);
cout<<"  ------------------------------";
gotoxy(1,11);
cout<<"  ------------------------------";
gotoxy(i,j); cout<<"*";
gotoxy(3,2);cout<<"|";
gotoxy(3,3);cout<<"|";
gotoxy(3,4);cout<<"|";
gotoxy(3,5);cout<<"|";
gotoxy(3,6);cout<<"|";
gotoxy(3,7);cout<<"|";
gotoxy(3,8);cout<<"|";
gotoxy(3,9);cout<<"|";
gotoxy(3,10);cout<<"|";
gotoxy(32,2);cout<<"|";
gotoxy(32,3);cout<<"|";
gotoxy(32,4);cout<<"|";
gotoxy(32,5);cout<<"|";
gotoxy(32,6);cout<<"|";
gotoxy(32,7);cout<<"|";
gotoxy(32,8);cout<<"|";
gotoxy(32,9);cout<<"|";
gotoxy(32,10);cout<<"|";
gotoxy(4,5);cout<<"|";
gotoxy(4,6);cout<<"|";
gotoxy(4,7);cout<<"|";
gotoxy(31,5);cout<<"|";
gotoxy(31,6);cout<<"|";
gotoxy(31,7);cout<<"|";
gotoxy(20,y)  ; cout<<"0";
gotoxy(20,y+1); cout<<"|";
gotoxy(20,y+2); cout<<"0";
gotoxy(20,y+3); cout<<"|";
gotoxy(20,y+4); cout<<"0";
gotoxy(20,y+5); cout<<"|";
gotoxy(20,y+6); cout<<"0";
gotoxy(10,t)  ; cout<<"0";
gotoxy(10,t+1); cout<<"|";
gotoxy(10,t+2); cout<<"0";
gotoxy(10,t+3); cout<<"|";
gotoxy(10,t+4); cout<<"0";
gotoxy(25,g)  ; cout<<"X";
gotoxy(25,g+1); cout<<"|";
gotoxy(25,g+2); cout<<"X";
gotoxy(25,g+3); cout<<"|";
gotoxy(25,g+4); cout<<"X";
gotoxy(15,h)  ; cout<<"X";
gotoxy(15,h+1); cout<<"|";
gotoxy(15,h+2); cout<<"X";
gotoxy(15,h+3); cout<<"|";
gotoxy(15,h+4); cout<<"X";
gotoxy(15,h+5); cout<<"|";
gotoxy(15,h+6); cout<<"X";
gotoxy(5,r)  ; cout<<"0";
gotoxy(30,f)  ; cout<<"X";
gotoxy(3,15);cout<<"Team A:"<<sc1<<"  Team B:"<<sc2;}
void main()
{clrscr();
int sc1=0,sc2=0,y=3,t=4,r=6,h=3,g=4,f=6,i=17,j=5,k=0,v=0; char a,s;
for (i=17,j=5;i<=30,i>=3,j>=3,j<=10;)
{delay(300);
display(y,t,r,h,g,f,i,j,sc1,sc2);
if(i==3){if(j==4||j==5||j==6){k=0;i=17;j=5;sc1++;
gotoxy(15,4);cout<<"        ";gotoxy(15,5);cout<<" GOAL!! ";gotoxy(15,6);cout<<"      ";getch();}}
if(i==31){if(j==4||j==5||j==6){k=1;i=17;j=5;sc2++;
gotoxy(15,4);cout<<"        ";gotoxy(15,5);cout<<" GOAL!! ";gotoxy(15,6);cout<<"      ";getch();}}
if(sc1==5){gotoxy(3,16);cout<<"GAME OVER!!  TEAM A WINS!!";getch();break;}
if(sc2==5){gotoxy(3,16);cout<<"GAME OVER!!  TEAM B WINS!!";getch();break;}
if(j==10)v=1;
if(j==2)v=0;
if(v==0) j++;
if(i==4)k=0;
if(i==31)k=1;
if(v==1) j--;
if(k==0) i++;
if(k==1) i--;
if(i==21&&v==0&&k==1)
{if(j==y-1||j==y+1||j==y+3||j==y+5){v=1;k=0;}}
if(i==11&&v==0&&k==1)
{if(j==t-1||j==t+1||j==t+3){v=1;k=0;}}
if(i==6&&v==0&&k==1)
{if(j==r-1){v=1;k=0;}}
if(i==16&&v==0&&k==1)
{if(j==h-1||j==h+1||j==h+3||j==h+5){v=1;k=0;}}
if(i==26&&v==0&&k==1)
{if(j==g-1||j==g+1||j==g+3){v=1;k=0;}}
if(i==31&&v==0&&k==1)
{if(j==f-1){v=1;k=0;}}

if(i==19&&v==0&&k==0)
{if(j==y-1||j==y+1||j==y+3||j==y+5){v=1;k=1;}}
if(i==9&&v==0&&k==0)
{if(j==t-1||j==t+1||j==t+3){v=1;k=1;}}
if(i==4&&v==0&&k==0)
{if(j==r-1){v=1;k=1;}}
if(i==14&&v==0&&k==0)
{if(j==h-1||j==h+1||j==h+3||j==h+5){v=1;k=1;}}
if(i==24&&v==0&&k==0)
{if(j==g-1||j==g+1||j==g+3){v=1;k=1;}}
if(i==29&&v==0&&k==0)
{if(j==f-1){v=1;k=1;}}

if(i==21&&v==1&&k==1)
{if(j==y+1||j==y+3||j==y+5||j==y+7){v=0;k=0;}}
if(i==11&&v==1&&k==1)
{if(j==t+1||j==t+3||j==t+5){v=0;k=0;}}
if(i==6&&v==1&&k==1)
{if(j==r+1){v=0;k=0;}}
if(i==16&&v==1&&k==1)
{if(j==h+1||j==h+3||j==h+5||j==h+7){v=0;k=0;}}
if(i==26&&v==1&&k==1)w
{if(j==g+1||j==g+3||j==g+5){v=0;k=0;}}
if(i==31&&v==1&&k==1)
{if(j==f+1){v=0;k=0;}}


if(i==19&&v==1&&k==0)
{if(j==y+1||j==y+3||j==y+5||j==y+7){v=0;k=1;}}
if(i==9&&v==1&&k==0)
{if(j==t+1||j==t+3||j==t+5){v=0;k=1;}}
if(i==4&&v==1&&k==0)
{if(j==r+1){v=0;k=1;}}
if(i==14&&v==1&&k==0)
{if(j==h+1||j==h+3||j==h+5||j==h+7){v=0;k=1;}}
if(i==24&&v==1&&k==0)
{if(j==g+1||j==g+3||j==g+5){v=0;k=1;}}
if(i==29&&v==1&&k==0)
{if(j==f+1){v=0;k=1;}}

if(i==20){if(j==y-1||j==y+1||j==y+3||j==y+5) v=1;}
if(i==10){if(j==t-1||j==t+1||j==t+3) v=1;}
if(i==5){if(j==r-1) v=1;}
if(i==20){if(j==y+1||j==y+3||j==y+5||j==y+7) v=0;}
if(i==10){if(j==t+1||j==t+3||j==t+5) v=0;}
if(i==5){if(j==r+1) v=0;}
if(i==15){if(j==h-1||j==h+1||j==h+3||j==h+5) v=1;}
if(i==25){if(j==g-1||j==g+1||j==g+3) v=1;}
if(i==30){if(j==f-1) v=1;}
if(i==15){if(j==h+1||j==h+3||j==h+5||j==h+7) v=0;}
if(i==25){if(j==g+1||j==g+3||j==g+5) v=0;}
if(i==30){if(j==f+1) v=0;}


if(i==21){if(j==y||j==y+2||j==y+4||j==y+6) k=0;}
if(i==11){if(j==t||j==t+2||j==t+4) k=0;}
if(i==6){if(j==r) k=0;}
if(i==19){if(j==y||j==y+2||j==y+4||j==y+6) k=1;}
if(i==9){if(j==t||j==t+2||j==t+4) k=1;}
if(i==14){if(j==h||j==h+2||j==h+4||j==h+6) k=1;}
if(i==24){if(j==g||j==g+2||j==g+4) k=1;}
if(i==29){if(j==f) k=1;}
if(i==16){if(j==h||j==h+2||j==h+4||j==h+6) k=0;}
if(i==26){if(j==g||j==g+2||j==g+4) k=0;}
if(kbhit())
{a=getch();
if(a=='2'&&y<4) {y++;v=1;}
else if(a=='8'&&y>2) {y--;v=0;}
if(a=='2'&&t<6) {t++;v=1;}
else if(a=='8'&&t>2) {t--;v=0;}
if(a=='2'&&r<10){r++;v=1;}
else if(a=='8'&&r>2) {r--;v=0;}
if(a=='s'&&h<4) {h++;v=1;}
else if(a=='w'&&h>2) {h--;v=0;}
if(a=='s'&&g<6) {g++;v=1;}
else if(a=='w'&&g>2) {g--;v=0;}
if(a=='s'&&f<10) {f++;v=1;}
else if(a=='w'&&f>2) {f--;v=0;}
}
}
getch();
}
Last edited on
Pages: 12