0

I am making a game in Visual studious 2017 (visual c++), where you have to repeatedly press the space bar to earn money. But I have run into a problem, the compiler can't keep up when you press the spacebar really fast and so It miscounts. I tried doing some research, but all I got was this and as I said earlier it cant keep up.

this is so far what i got:

   int click_systm()
 {
char spacebar;
while (1)
{
    if (GetAsyncKeyState(VK_SPACE) != 0)
    {
        if (GetKeyState(VK_SPACE) == 0)
        {
            spacebar = _getch();

            int value = spacebar;
            if (value == 32)
            {
                money++;
                cout << money << endl;
                

            }
        }
    

    }
}


   }

If you made it here, thanks for taking your time to read this :)

cool bean
  • 11
  • 3
  • You are making a console application? I think this is the wrong framework in which to make this type of game. You can make it as a normal (not console) application and then you can receive an event every time the key is pressed. But with a console application, the console interprets the keys for you and you end up with this ugly loop that doesn't work right, and possibly can't be fixed. – user253751 Jul 16 '21 at 11:13
  • You are delaying your program with `cout << money << endl`. Maybe that's the problem (or maybe not). (And instead of `endl` use `\n`, `endl` flush the stream) – Antonio Jul 16 '21 at 11:16
  • you may want to run the screen output in another thread and just post to it from the main thread instead of writing directly to cout in your loop. using cout is notoriously slow so input may be lost. – AndersK Jul 16 '21 at 11:21
  • `_getch` returns an **int**, not `char`. You must change to `int spacebar` and check for EOF. See [Why is getchar() function in C an Integer?](https://stackoverflow.com/q/39341213/995714), [Why must the variable used to hold getchar's return value be declared as int?](https://stackoverflow.com/q/18013167/995714) – phuclv Jul 16 '21 at 14:36

2 Answers2

0

The compiler is what compiles your C++ code into some binary. The CPU as well as the OS should be able to keep up with a human speed spacebar smashing. The problem can be from the fact that you run your soft inside a console(please see the comment left by user253751).

std::cout slows down a program by a lot because of the time it takes to print. As also mentioned it the comment, split your printing in a different thread. You can also add std::ios_base::sync_with_stdio(false); to gain some speed if you really need to.

R3DC0DE
  • 19
  • 7
  • I don't think it's std::cout; I think this loop design with GetAsyncKeyState is just unsuitable for this purpose. – user253751 Jul 16 '21 at 11:55
0

I have found an answer to my own question, for those that have the same problem. This code here will do the same thing but is more responsive and will keep up to the button mashing.

int click_systm()
{
 char spacebar;
while (1)
{
    
            spacebar = _getch();

            
            if (spacebar == 32)
            {
                money++;
                
                    cout << money << "\n";

    }
}


}

Thanks to everyone trying to help me, espically MrScriptX and Antonio.

cool bean
  • 11
  • 3