Read a single character in C

4

I was doing a menu of options and I ran into a problem.

When I put "a" and press enter I go to option a but if I put "a3214" it also takes me to option a. My question is if I can avoid that and show an error message if it is not only the option I write to

int main()
{
    char option;

    printf("Write a single character\n");
    do
        option=getchar();
    while(option!='a' && option!='b'&& option!='c' );

    switch(option)
    {
        case 'a': printf("Hello\n");
        break; 
        case 'b': printf("How are you\n");
        break;
        case 'c': printf("Bye\n");
        break;
    }
    return 0;
}
    
asked by Luis 12.11.2016 в 16:48
source

2 answers

1

The cleanest solution is to use fgets , fgets allow to store in a buffer what we write in stdin , you just have to check that the first character is a , b or c and that the second character is an intro ( '\n' ), in this way you make sure that the user has not written something wrong as "a3214" :

#include <stdio.h>

int main(void)
{
    char buf[128];
    char option;

    printf("Write a single character\n");
    while (fgets(buf, sizeof buf, stdin)) {
        option = buf[0];
        if (option == 'a' || option == 'b' || option == 'c' ) {
            if (buf[1] == '\n') break;
        }
        puts("Introduzca un solo carácter: [a b c] seguido de un intro.");
    }   
    switch(option)
    {
        case 'a': printf("Hello\n");
        break; 
        case 'b': printf("How are you\n");
        break;
        case 'c': printf("Bye\n");
        break;
    }
    return 0;
}

Another important detail to keep in mind is that it is always convenient to initialize the variables. In this particular case it is not guaranteed that fgets will be executed successfully (you can check it by launching the program with cat archivo_vacio | ./programa ), later, when reading the variable without initializing option in switch(option) we will be accessing some sector containing " trash ", at this moment the behavior of the program is unpredictable, to heal you in health:

    char option = 0;
    
answered by 12.11.2016 / 18:43
source
1

Your code does not make sense:

getchar( ) - > read 1 and only one, for many more than pulses.

while( ... ) - > I guess you want to wait until a correct character is pressed, but, since you have left the getchar( ) out of the loop, either it enters an infinite loop, or it does not do anything (if the character is 'a', 'b' , or 'c').

switch( ) { ... } - > It's the only thing that seems to be fine.

You already have enough clues to go pulling ...

EDITO

I am ashamed to admit that I did not see the do . I'll be getting old.

Having that do in mind, the thing changes a lot. Now if I see him I mean this, and your question, if I understood it correctly.

Do you want me to recognize the pressing of the 1st key, without having to press ENTER?

Short answer, for a change: you can not . The I / O functions wait until you press ENTER. There's nothing to do (in Linux, you could put the terminal in raw mode, but that's another story).

EDIT 2

With the new explanation of the user, the thing has become clearer. What you want to do is read a string and, if you have more than 1 character , indicate the error.

  • You can not use use getchar . Use fgets or scanf .
  • If you use the one, you will need a buffer to store the string. There is thrice questions about arrays and arrays on this site, anyone can serve you.
  • You have to get the length of the string read. If the length is > 1 , it is a user error and treat it as such.
  • Otherwise, check the first character of the string.
  • answered by 12.11.2016 в 17:01