I am following a tutorial video on how to make a very simple game using C++. I am very early in the tutorial and I had no issues until now. Accourding to the video when I run the program it should display any key I press with “Here’s what you pressed: (pressed key goes here)”. Also, it should exit the program when I press the Q key. On the video it works fine, but sadly on my screen it is just a blank DOS prompt that does not respond to anything. Can anyone please look at what I got so far and see if there is a way to troubleshoot this issue. Again, I am new at this so any help would be greatly appreciated. Perhaps there is a header missing or something…
game.cpp
#include <iostream> //Include this and namespace in all files.
using namespace std;
#include "game.h"
#include <conio.h>
bool Game::run(void)
{
char key = ' ';
while (key != 'q')
{
while (!getInput(&key))
{
}
cout << "Here's what you pressed: " << key << endl;
}
cout << "End of the game" << endl;
return true;
}
bool Game::getInput(char *c)
{
if (kbhit())
{
*c = getch();
}
return false;
}
game.h
#ifndef GAME_H //Make sure this accompanies #endif.
#define GAME_H
class Game
{
public:
bool run (void);
protected:
bool getInput (char *c);
void timerUpdate (void);
};
#endif //Make sure this accompanies #ifndef.
main.cpp
#include "game.h"
int main ()
{
Game gameHeart;
gameHeart.run();
return 0;
//system("pause");
}
The
Game.getInputalways returnsfalse, soGame.runwould endlessly ask for a keyboard input. Here’s the fix.