+ 2

check char in C

// the app should read one character scanf("%c", &character); // if the user input is longer as one character, or a number which is outside of my if-definition // the the code should reask the input if(c==longerThenOneChar && c <65 && c>90) // wrong input, please input a new char scanf("%c", &character); // how can i check this

8th Mar 2019, 5:04 PM
Maxim Schiffmann
Maxim Schiffmann - avatar
4 Antworten
+ 2
how i find out, if c is longer then one char?
8th Mar 2019, 8:00 PM
Maxim Schiffmann
Maxim Schiffmann - avatar
+ 1
You cannot test if its longer than one char this way, because you are only reading the first char. To do that you will need to read until the end of the input, then test if its more than 1 char. Or just test for a second char.
8th Mar 2019, 9:39 PM
Jared Bird
Jared Bird - avatar
8th Mar 2019, 9:59 PM
unChabon
unChabon - avatar
0
Use do{ } while(); do { scanf("%c", &character); while(c == longerThenOneChar || c <65 || c>90) ; if you want to show messages asking for input: e = 0; do{ if (e > 0) { printf("error. input again"); } else { printf("Input character"); e = 1; } scanf("%c", &character); }while(c == longerThenOneChar || c <65 || c>90) ; ...just one of many versions... :)
8th Mar 2019, 7:28 PM
unChabon
unChabon - avatar