1

I'm having trouble getting my program to accept hitting the enter key to continue... I tried getchar() two ways (as condition for while loop and just calling the routine) but one doesn't pause the program and await input it just goes through it all and the other pauses but wont let me enter anything or continue when i hit enter...

void RestOfGame(int r, int c, int mG, int **cGen,int **nGen) {
  int i = 0, q = 0;
  for (i = 2; i <= mG; i++) {
    printf("\n(press enter to continue)\n\n");
    getchar();
    printf("Generation %d:\n", i);
    ExamineReplace(r, c, cGen, nGen);
    NewToOld(r, c, cGen, nGen);
    DrawGrid(r, c, cGen);
  }
}

I read in a few other threads something about having previously scanned in something and the buffer keeping the \n... tried some variations but I dont think i did it right.

Working on a windows platform, C language. Any suggestions?

UPDATE: Thought I might add that this code is being run with a data file as input in redirection at command line. Would confuse my getchar() even though I've read through the entire file?

Bruno
  • 11
  • 2
  • If this is a game it's much better to use operating system events to capture key presses and avoid the C library functions entirely. If it's a command-line game use something like NCurses to drive your rendering. – tadman Feb 04 '20 at 04:10
  • Just happens to be the name of the function, not game programming. – Bruno Feb 04 '20 at 04:13
  • 2
    This one appears easy but could be a real pain. Questions for you to consider: (1) Which platform are you working on? The console behavior might differ between Windows and Linux. (2) Are you expecting an `Enter` or just any key? (3) What if the user does this when you tell him to press enter: he pressed `abcd[Enter]`? – aafulei Feb 04 '20 at 04:13
  • Relevant: https://stackoverflow.com/a/34247021/7670262 – Azeem Feb 04 '20 at 05:02
  • Regarding your update. Just Print-out getchar to see if there is anything read. – HWilmer Feb 04 '20 at 06:48

2 Answers2

1

Use of the GetAsyncKeyState() function from the Windows.h header file allows us to check if a key has been pressed or currently being pressed.

So create a loop that checks the key state until it detects that it has been pressed. which I have done in the wait_key() function

#include <stdio.h>
#include <stdlib.h>

#include <Windows.h>

#define UNUSED(x)((void)(x))

// Function that waits for key press
static inline void wait_key(const int vKey, const char* const phrase) {
    // Display user message
    puts(phrase);

    // Wait until key is pressed
    while (!GetAsyncKeyState(vKey))
        Sleep(100);
}

int main(const int argc, const char** argv) {
    UNUSED(argc); UNUSED(argv);

    // VK_RETURN = enter key : https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
    // Wait until enter key is pressed
    wait_key(VK_RETURN, "Press enter to continue...");

    return (int)EXIT_SUCCESS;
}
Matt G.
  • 21
  • 6
0

If you simply want to wait and have the user to press Enter to continue, then use fgets() and a short buffer. fgets() will block until input is received and will read and include the '\n' (generated by pressing Enter) in the buffer it fills.

You must be mindful of the state of stdin before you call fgets() (e.g. if a prior read with scanf and "%d" has left a '\n' in stdin unread, then it will appear that fgets() was skipped)

Your function above and be re-written as follows to wait until the user presses Enter

void RestOfGame (int r, int c, int mG, int **cGen,int **nGen)
{
    int i = 0, q = 0;

    for (i = 2; i <= mG; i++) {
        char buf[128];                                    /* short buffer for fgets */
        printf("\n(press enter to continue)\n\n");
        fgets (buf, sizeof buf, stdin);                   /* fgets for input */
        printf("Generation %d:\n", i);
        ExamineReplace(r, c, cGen, nGen);
        NewToOld(r, c, cGen, nGen);
        DrawGrid(r, c, cGen);
    }
}

Above, buf is a short 128-char buffer that will handle the Enter as well as 126 other slips of the finger before Enter and continue working fine.

David C. Rankin
  • 69,681
  • 6
  • 44
  • 72