check for correct user input
how can we check that the user i/p is correct through a c code.. for ex we are asking name and age from user i/p, then what should be the syntax for check that whether t开发者_如何学JAVAhe data entered is correct or not? the code should be in C
For the name, store the user input in a string. Traverse the string and say error if array elements are not 'a' to 'z' or 'A' to 'Z'. As for as age is considered, you can use char array or integer. While using integer if an user enters 'a' the ascii value of that will be taken. But if you use char array you can see only for digits and with a limit.
int main()
{
char name[10],age[2];
int i;
printf("Enter name and age \n");
scanf("%s %s",name,age);
for(i=0; name[i]!=NULL; i++)
if(isdigit(name[i]))
printf("Error, name in aplahbets\n");
for(i=0; age[i]!=NULL; i++)
if(isalpha(age[i]))
printf("Error, age in numbers\n");
// Rest of your code Your code
return 0;
}
Age is typically a natural number, ranging from 0 to 130. Depending on your application, a more realistic range might be 15 to 65. Take a look at your requirements.
int IsNaturalNumber(const char* number)
{
while(*number) {
if(!isdigit(*number))
return 0;
++number;
}
return 1;
}
char age[20+1] = { 0 };
if(!fgets(age, 20, stdin)) error("EOF reached!");
if(!IsNaturalNumber(age)) error("Please enter a natural number!");
int age_as_int = atoi(age);
if(age_as_int < LOWER_AGE_BOUND || age_as_int > UPPER_AGE_BOUND)
error("Age must be between %d and %d.", LOWER_AGE_BOUND, UPPER_AGE_BOUND);
Names are tricky. What is a proper name? A commonly used form is Salutation (Mr./Mrs./Miss), First name and Last/Family name. This covers a lot of cultures. Don't forget to take language into consideration. Mr. = Herr, Mrs. = Frau, Miss = Fräulein in German for instance. Also, some cultures prefer family names before first name in forms (Japan for instance). What are your requirements? Do you need to consider all cultures, or just a few?
The common way to do input validation is to read the input as a string into the program and then to validate it and convert to the expected type.
For names, there is not much useful validation you can do. You can (and should) check that the input does not exceed your expectations, but otherwise most input should be regarded as a valid name.
For age, there is a lot more validation you can do. First the input must be a valid integer and secondly, it must be within a certain range.
You could use the following functions:
char* readString(char* buffer, size_t buflen)
{
char* result;
result = fgets(buffer, buflen, stdin);
if ((result != NULL) &&
(strlen(result) == buflen-1) &&
(result[buflen-2] != '\n'))
{
/* too long a line */
/* read the remainder of the line, to clear the input buffers */
while ((result != NULL) && (result[strlen(result)-1] != '\n'))
{
result = fgets(buffer, buflen, stdin);
}
/* set result to NULL to indicate an error */
result = NULL;
}
if (result != NULL)
{
/* strip the newline */
result[strlen(result)-1] = '\0';
}
}
bool readInt(int low, int high, int* value)
{
char temp[80];
if (readString(temp, 80))
{
char* endp;
long result = strtol(temp, &endp, 10);
if ((*endp == '\0') && (result >= low) && (result <= high))
{
*value = result;
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
Without any age-overflow/underflow check a short solution could be:
char name[40];
unsigned age;
if( scanf("%39s%u",name,&age)==2 )
printf("name=%s age=%u",name,age);
else
puts("input invalid");
You should ever check the returns from any user-inputs.
精彩评论