| | |
Need Help With Error Checking User Input
![]() |
•
•
Join Date: Dec 2004
Posts: 1
Reputation:
Solved Threads: 0
I am trying to write a program that takes a series of numbers input by a user and performs some calculations on it. While I have most of the code put together and working perfectly fine, I haven't been able to write code that will refuse the users input if it is a number followed by letters (ie., "12a" or "12f345"). For some reason, this causes the program to close. Below is the snippet of code isolated to test with.
How can I get the code to display a message that the user input was not a number, to ask for a valid number, and to not close the program?
Thank you, All...
#include <stdio.h>
int main(void)
{
float num1;
printf("Please enter a number:");
while(! scanf("%f", &num1))
{
printf("\n\tInvalid entry, please try again: ");
fflush(stdin);
}
printf("The number you entered was %.2f\n", num1);
getChar();
}
How can I get the code to display a message that the user input was not a number, to ask for a valid number, and to not close the program?
Thank you, All...
#include <stdio.h>
int main(void)
{
float num1;
printf("Please enter a number:");
while(! scanf("%f", &num1))
{
printf("\n\tInvalid entry, please try again: ");
fflush(stdin);
}
printf("The number you entered was %.2f\n", num1);
getChar();
}
Robert A. Inot
One way to do this, which is easiest on the user, is to extract the numeric content of the entry and go on. Here is a simple code snippet:
[php]// A foolproof input routine for numbers:
// The input string is analyzed for nondigit characters
// which are removed, the resulting cleaned string is
// then converted to a real number with atof()
// converts entries like -$12.34, 98F or 345.2KG
// coded using free Pelles C
#include <stdio.h>
#include <ctype.h> // isdigit()
#include <string.h> // strlen()
#include <stdlib.h> // atof()
#define EOS 0 // end of string marker
double real_in(void);
int main()
{
double db;
printf("\n(Enter a zero to exit)\n");
do
{
db = real_in();
printf("\nNumeric content = %f\n",db);
} while(db != 0);
return 0;
}
// enter the value as string then process to float
double real_in()
{
static char s1[25], s2[25];
int k, n = 0;
printf("\nEnter a number : ");
gets(s1);
for (k = 0; k < strlen(s1); k++)
// retain - and .
if (isdigit(s1[k]) || s1[k] == '.' || s1[k] == '-') {
s2[n] = s1[k];
n++;
}
s2[n] = EOS;
// convert string to float and return
return (atof(s2));
}
[/php]
[php]// A foolproof input routine for numbers:
// The input string is analyzed for nondigit characters
// which are removed, the resulting cleaned string is
// then converted to a real number with atof()
// converts entries like -$12.34, 98F or 345.2KG
// coded using free Pelles C
#include <stdio.h>
#include <ctype.h> // isdigit()
#include <string.h> // strlen()
#include <stdlib.h> // atof()
#define EOS 0 // end of string marker
double real_in(void);
int main()
{
double db;
printf("\n(Enter a zero to exit)\n");
do
{
db = real_in();
printf("\nNumeric content = %f\n",db);
} while(db != 0);
return 0;
}
// enter the value as string then process to float
double real_in()
{
static char s1[25], s2[25];
int k, n = 0;
printf("\nEnter a number : ");
gets(s1);
for (k = 0; k < strlen(s1); k++)
// retain - and .
if (isdigit(s1[k]) || s1[k] == '.' || s1[k] == '-') {
s2[n] = s1[k];
n++;
}
s2[n] = EOS;
// convert string to float and return
return (atof(s2));
}
[/php]
May 'the Google' be with you!
You must be careful when you advertise "foolproof".
- Never use gets(). That immediately makes the code foolprone.
- There are a few other characters that might be legal in a double ('+', 'e', and 'E' come to mind). The standard functions handle this, whereas this function does not.
- I don't know if "cleaning" the string is the best approach. I would think that if invalid characters are found, the user should be re-prompted to enter correct information -- rather than making possibly unwarranted assumptions about user intent.
•
•
•
•
Originally Posted by Dave Sinkula
You must be careful when you advertise "foolproof".
- Never use gets(). That immediately makes the code foolprone.
- There are a few other characters that might be legal in a double ('+', 'e', and 'E' come to mind). The standard functions handle this, whereas this function does not.
- I don't know if "cleaning" the string is the best approach. I would think that if invalid characters are found, the user should be re-prompted to enter correct information -- rather than making possibly unwarranted assumptions about user intent.
Foolproof, I admit, depends on the pool of fools. I was looking at a bunch of PhDs rather than lawyers.
Yes, by all means improve this simple code example. The user should be informed in some nice fashion!
Thanks for your always august opinions Dave. I do treasure them.
I hope Narue comes back from her vacation soon!
May 'the Google' be with you!
![]() |
Similar Threads
- Newbie Question: Error Checking User Input (C++)
- Error Checking for user input (Java)
- error checking of user input (C++)
Other Threads in the C Forum
- Previous Thread: MORE filter in UNIX
- Next Thread: Graphics In Pixel: Part II B
| Thread Tools | Search this Thread |
adobe ansi api array asterisks binarysearch calculate centimeter changingto char convert copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile createprocess() csyntax database directory fflush file fork forloop frequency givemetehcodez global grade graphics gtkgcurlcompiling hacking highest histogram homework i/o inches infiniteloop input interest iso kernel kilometer km linked linkedlist linux linuxsegmentationfault list locate looping loopinsideloop. lowest match meter microsoft mysql number open opendocumentformat openwebfoundation owf pattern pdf performance posix power probleminc process program programming pyramidusingturboccodes radix read recv repetition research reversing scanf scheduling segmentationfault send sequential socket socketprograming stack standard string suggestions systemcall threads turboc unix user variable voidmain() wab win32api windows.h windowsapi






