Hi folks,
Every time a user inputs anything other than a number in my programs it goes into a mad scroll…..is there a way to stop this….so that they would get an error message if the input was not a number
Thanks fer any help…..
Printable View
Hi folks,
Every time a user inputs anything other than a number in my programs it goes into a mad scroll…..is there a way to stop this….so that they would get an error message if the input was not a number
Thanks fer any help…..
>>> is there a way to stop this
Yes.
What do you mean, not helpful! My answer gives as much information as your question!!!
Well, let me just say that I ALWAYS grab input as strings! That is just my preference, and I have no problem converting numerical strings into ints, floats, etc.
That being said, you could adopt this approach and go further with the validation process.
For instance, you could write a function which validates that a string is in fact all numbers:
Code:int validate(char *input)
{
int len = strlen(input);
int i;
for( i = 0; i < len; i++)
{
if(!isdigit(input[i]))
return 0;
}
return 1;
}
Then in main you could do:
Hope that helps!Code:int main()
{
char input[100];
int num;
do{
clrscr();
printf("Enter A Valid Number: \n\n");
fgets(input, 100, stdin);
if( !validate(input) )
continue;
break;
}while(1);
num = atoi(input);
/// rest of program...
Thx for the help Sebastiani.............