0

I have a do-while loop, and I want if I press ENTER key, the progress will continue, but q will finish the program. It is not working as the program will end straight away and does not wait for the user to enter the key.

Code below is my main code.

void displayGrid() {
    bool progress = true;
    printf("%s", "input round for round mode, moves for move mode");
    scanf("%s", input);
    toLowerCase(input);
    if (strcmp(input, "round") == 0) {
        do {
            printf("Enter key ENTER to continue,Q for quit \n");
            bool qoc = quitOrContinue();
            if (qoc) {
            } else if (!qoc) {
                progress = false;
            }
        } while (progress);
    }
}

This is my code for checking enter and q key:

bool quitOrContinue() {
    if (kbhit()) {
        char click = fgetc(stdin);
        while (getchar() != '\n');
        if (click == 0x0A) {
            return true;
        } else if (click == 'q') {
            return false;
        }
    }
}
hyde
  • 60,639
  • 21
  • 115
  • 176
BZS
  • 1
  • 1
  • 1
    What is `while (getchar() != '\n');` doing in your code? Seems like it's redundant. – Dawid Jan 01 '23 at 15:17
  • Why the `kbhit()`? – user3840170 Jan 01 '23 at 15:19
  • Because checking if the key been hit – BZS Jan 01 '23 at 15:24
  • You have a `while` loop on one line that reads up to a newline. Then you check whether the newline is still a newline. You need to rethink. The semicolon at the end of the one-line `while` loop is probably bogus; you should then indent the `if` statement as the body of the loop, preferably enclosed in braces. Note that since you've not jiggered the terminal settings visibly, you won't see any input until you've hit the enter key. The only question is "was there a q before the newline". There is also EOF to take into consideration. – Jonathan Leffler Jan 01 '23 at 15:52
  • Added conio tag based on use of `kbhit()`. Please remove if it is wrong. – hyde Jan 01 '23 at 15:53
  • If you use conio, you should probbably use `getch()` instead of `getchar()`. – hyde Jan 01 '23 at 15:55
  • 4
    I don't understand the purpose of using ```kbhit```, ```fgetc```, and ```getchar``` all at once, when any one would do the job. And what exactly is that ```while``` loop doing? – Harith Jan 01 '23 at 16:00

1 Answers1

1

You do not need three functions to read a char from stdin. Here's some psuedo-code to illustrate how to read one char. (I couldn't test it, so there may be some bugs in it).

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

static bool quitOrContinue(void)
{
    int click = fgetc(stdin);
    if (click == 0x0A) {
        return true;
    } else if (click == 'q') {
        return false;
    }
    /* Returns false in case of any other character */
    return false;
} 

int main(void) 
{
    bool condition = false;
    do {
        printf("Hello World\n");
        printf("Enter q to quit or ENTER to continue.\n");
        condition = quitOrContinue();
    } while (condition);

    return EXIT_SUCCESS;
}

You do not need the progress variable.

while (getchar() != '\n'); serves no purpose in your code, unless you're trying to flush stdin.

regarding:

printf("%s", "input round for round mode, moves for move mode");

You could use:

printf("input round for round mode, moves for move mode");

regarding:

scanf("%s", input);

What happens when one inputs more than size characters?

Limit length:

scanf("%6s", input);
Harith
  • 4,663
  • 1
  • 5
  • 20
  • After pressing enter for "round" input, the program automatically printed out twice, and then it stopped working(the programming is still running but it doesn't respond to my key press) – BZS Jan 01 '23 at 18:50
  • I believe it's ```scanf``` leaving the newline in the buffer that automatically gets read by subsequent calls to input functions. – Harith Jan 01 '23 at 18:54
  • See https://stackoverflow.com/q/5240789/20017547 – Harith Jan 01 '23 at 18:55