do{
char reply;
printf("Enter your choice > ")
scanf("%c", &reply)
if (isdigit(reply) != 0){
//inside are my contents not really important
//what I want to ask is, if user input 123, because it is a char, hence 1 will be taken but it is a wrong input. How can i check it and print a "invalid input"?
}
}while (reply != 3)
Asked
Active
Viewed 31 times
0

Jabberwocky
- 48,281
- 17
- 65
- 115

Ang Valen
- 1
- 1
-
1you can ask `scanf()` to read more than 1 char, with %s. That would allow you to check the length of the read string, then if correct, use isdigit() for the 1st char. – Angevil Mar 04 '21 at 13:22
-
2What he said, but don't forget to watch out for buffer overflows, since `%s` will read as many characters as are written, and can easily overflow your char buffer. Prefer `fgets` than `scanf()` – Adalcar Mar 04 '21 at 13:23
-
@Angevil ahh Thank you so much, I solved it just now. Thank you! – Ang Valen Mar 04 '21 at 13:33
-
@Adalcar I see... Thank you too – Ang Valen Mar 04 '21 at 13:34
-
Remember too that [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). – Weather Vane Mar 04 '21 at 14:36