Why does it keep going??? (kbhit())?

My program is too big to put here, but this is basically the problem. The user must press J to interact, and it is supposed to wait for every single time J is pressed to output the next number, but instead it just keeps going. Isn't kbhit() supposed to wait for a key to be hit??

If there isn't a problem with this particular code, what do you think may be causing an almost similar coding to act that way? It just keeps going and ignores the getch. It ignores getch yet it keeps outputting numbers, which MAKES NO SENSE.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int main()
{
  int index=0;
  char option;

 cout<<"Enter a J to interact:";

  while(true)
  {
    if(kbhit())
    {
      option=getch();
 
      if(option=='J')
      {
        cout<<index<<" ";
        index++;
      }
    }

  }

}


Edit: Ok so I tested this code and it works as it should, but mine still keeps going. I would like to know if anyone would be interested in seeing a bigger piece of code, from my actual program.

Solved
Last edited on
I'm curious, what was the problem?
If it isn't solved, you may show it to me. Otherwise, good luck with your project.
closed account (Dy7SLyTq)
it makes perfect sense. its because kbhit is unbuffered input (i believe) so while you dont hit a key its not going to do anything but then when you do hit the key it will grab the number, but then getch grabs the number from kbhit
Topic archived. No new replies allowed.