You are here:

C++/c++ input check


I am a beginner programmer and I've been trying to code a small game (something similar to PONG). In the game loop, I've done something like this:

   if(direction=='character') //several conditions and events
 //Output here...just basic C++ graphics based on the events above.

My problem is that my game keeps waiting for a keystroke once it gets to that if(kbhit). I would like the output to be performed independent of input. (example: if I don't move the paddle, the ball should still move).

Do you have any solutions I might use? Please keep in mind the fact that I am a beginner.

If you need any more explanations of what I am trying to do, please ask and I could even pass the CPP file to you for a more detailed inspection (if you consider the problem to be coming from something other than the if(kbihit) ).

Thank you in advance for your time!

Alex, the problem is that
if (kbhit)
does not call the kbhit function, it simply returns the address of the function, which is not zero, and therefore true. You need to add the parenthesis.

Here is the program. I did it on windows, so I use the windows specific
Sleep(int milliseconds)
function to slow it down. If you're using linux, you want the unix
sleep(int seconds)
function instead.

Please read my comments in the program.

#include <conio.h>
#include <stdio.h>
#include <Windows.h> // For the Sleep function

int main()
   int done = 0; // or bool done = false
   while(!done) // not exit, exit is a function
       printf("Waiting for kbhit\n");
       if(kbhit()) // not if(kbhit), you need the parentheses to make the function call
         int direction=getch();
         if(direction=='x') // Only 1 character can be in single quotes.
         printf("Got x\n");


All Answers

Answers by Expert:

Ask Experts




No longer taking questions.


No longer taking questions.

No longer taking questions.

©2017 All rights reserved.

[an error occurred while processing this directive]