I am trying to create some code that will only accept valid that as user input, like no integers when asking for name etc…
I have tried this so far,
if(!gets(newCust.name))
{
printf("invalid data\n");
menu();
}
However, since name is of type string, when user inputs integers it seems to be valid. Is there any other simple option I could use ?
Validating input (especially from C) can be quite a bit of work. In general, the way I would do it is to read the buffer (you should maybe use fgets( …, stdin) instead of
getsto avoid buffer overflows). After reading the buffer (no matter what the input is), then write the code to validate the data that is in the buffer.For example, you could use strtol to read integer values from the buffer. That would allow you to determine if the buffer has an integer (and only an integer).
The following is the general idea: