How can I read one character from the console?

For example: "Are you sure? (D) a, (H) et."

Closed due to the fact that off-topic participants Viktorov , andreymal , MihailPw , user194374, dlarchikov Jan 5 '18 at 21:59 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reasons:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - andreymal, dlarchikov
  • " Learning tasks are allowed as questions only on condition that you tried to solve them yourself before asking a question . Please edit the question and indicate what caused you difficulties in solving the problem. For example, give the code you wrote, trying to solve a problem "- Viktorov, MihailPw, Community Spirit
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • What have you got so far? - 0xdb
  • one
    Nothing .....:) - Vladislav Anatolyevich
  • 2
    Like, even main () no? Try it, you still have time. - 0xdb
  • scanf () - read the character) Ps and correct the question, what exactly did not work out? - Andrew Tarasenko
  • Thank you, but scanf () just skips input :( - Vladislav Anatolyevich

3 answers 3

Nostradamlyu :) that before this somewhere there was a read in the spirit of scanf("%d" without a reset after this input buffer, so \n remains in it, which is read by the next scanf .

Check if you reset the buffer ( fflush nothing to do with it, if that), and if not, try resetting ...

Well, the second option is the wrong encoding, if you are reading Russian characters ...

On the advice of @AndrejLevkovitch: resetting the buffer means taking the rest of the line to nowhere :) - for example,

 for(int c = getchar (); c != '\n' && c != EOF; c = getchar ()); 
  • I will add that after any input, after which you need to count the characters, it is better to add the code: while (getchar ()! = '\ N') continue; - Andrej Levkovitch
  • @AndrejLevkovitch So this is the reset of the input buffer ... - Harry
  • I think that it is better to give the code - if he does not know that there are unread characters in the buffer, then it does not know how to skip them. - Andrej Levkovitch

Typically, keyboard input comes line by line into the program. In C, there is no standard portable way to read a single character from the console, without waiting for the Enter key. comp.lang.c FAQ: How can I use the RETURN key?

In other words: getchar() will not return until '\n' is encountered (which can happen after entering many many characters). In order not to wait for a new line to read a single character from the Windows console, you can _getche() / _getwche() from conio.h .

On Unix, you need to change the terminal mode in order to get character input and restore the previous settings on your own when you exit the program. For example, using the curses library, you can cbreak mode :

 #include <ctype.h> #include <curses.h> int main() { initscr(); cbreak(); // change terminal mode to accept a char at a time // get answer int c; do { mvaddstr(0, 0, "Are you sure? (Y)es/(N)o: "); c = getch(); c = toupper((unsigned char)c); } while (c != 'Y' && c != 'N'); endwin(); // restore terminal settings return c == 'N'; } 

Example:

 $ cc *.c -lcurses && ./a.out && echo yes || echo no 

    Let's say you need to find out the confirmation from the user and write to the variable for this purpose the function scanf(); and learn how to work with MANAGER SEQUENCE scanf ("%c",&i); writes one character to the variable i and here% s already several characters. To be sure, you can loop this into validating the data entered.

     do {scanf ("%c",&i);}while (!(i == 'Y')||(i == 'N')); 
    • Thank you, but scanf () for some reason misses input: ( - Vladislav Anatolyevich
    • Invalid code ... - Qwertiy ♦