Hi I need to prompt a user for some input and then validate it. The input must only be validated if it is a positive integer and not greater then 23. The only problem I am having with this is when the user enters a non-numerical input like "hello." The code below does not successfully detect that any input is non-numerical and though I have tried many methods to do this, none of them seem to work. Below is the closest I seem to have gotten by taking the input as a string then converting it to an integer, however it still does not work. Any help would be appreciated.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(void) {
int height;
char input[50];
int cont = 0;
while (cont == 0) {
printf("Please provide a non-negative integer no greater than 23.\n");
scanf("%s", &input);
height = atoi(input);
if (height <= 23 && height >= 0) {
cont = 1;
} else {
//do nothing
}
}
printf("Valid Input.\n");
return 0;
}
scanf("%s", &input);is not correct. Hint: what's the type ofinput?%sforscanfwithout a field width (and thesspecifier takes achar *argument, you passed achar (*)[50]argument):scanf("%49s", input).