![]() |
| ||
| Need Help With Error Checking User Input 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(); } |
| ||
| Re: Need Help With Error Checking User Input |
| ||
| Re: Need Help With Error Checking User Input 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] |
| ||
| Re: Need Help With Error Checking User Input You must be careful when you advertise "foolproof".
|
| ||
| Re: Need Help With Error Checking User Input Quote:
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! |
| All times are GMT -4. The time now is 8:48 pm. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC