Reputation: 11
My program uses a recursive function which either calls itself again or backs up to the previous call depending on conditions ... and it works perfectly. However, given that the program needs to run through 72^72 possible combinations, it will take several months to run to completion. I would like to be able to press a key and have it display the current values of an array on the console so that I can observe the progress on the program.
To do this, I put this code at the beginning of the recursive function.
if(kbhit())
{
displayArrayConsole(); //my function which displays the array on the console
//need something here to clear input buffer
}
This doesn't work as intended as the input buffer is never cleared. When I press a key, the array is printed at the start of every call to the recursive function, which is happening several hundred thousand times a second.
I tried adding fflush(stdin), but that didn't so anything. I tried adding clear() but this isn't recognized by my compiler (gcc).
Using getchar() does work, as it pulls the character out of the buffer, but it pauses the program until I hit enter. This solution will work, but ideally I'd like to be able to press a key to display the array and then have the program keep right on going without pausing until I press enter.
I've found a bunch of people here asking similar questions, but none of the solutions there seem to work for me. They are mostly all using while loops, which won't work for me due to the recursive nature of my program.
Can someone suggest a possible solution here?
Thanks
Upvotes: 0
Views: 22