Error Checking

• 02-08-2006
jlharrison
Error Checking
I need to make sure the user enters a number from the menu. The do while loop will ensure that I get the choice in the right range. I now just need to be sure that the number entered is not a decimal. I thought that the while loop with in would solve this problem but it does not. I want to know if there is something that I can do to loop the scanf untill I get a positive whole number in the range 1 through 6.
Code:

```do                 {                 printf ("Please enter a number from the menu.\n");                 printf ("1. Inches to centimeters\n");                 printf ("2. Feet to meters\n");                 printf ("3. Miles to kilometers\n");                 printf ("4. Pounds to kilograms\n");                 printf ("5. Gallons to liters\n");                 printf ("6. Quit the program\n");                 printf ("What Would You Like To Do?:");                         scanf ("%d", &menuChoice);                 while (!scanf("%d", &menuChoice));                         {                                printf ("Enter a choice 1, 2, 3, 4,  5, or 6.\n");                         scanf ("%d", &menuChoice);                         }                 } while ((menuChoice < 1) || (menuChoice > 6));```
Thanks
Justin
• 02-08-2006
Dave_Sinkula
• 02-08-2006
jlharrison
I think you are refering to OPTION 4 Custom Validation.

That seems way ahead of where I am in class right now. We havent covered arrays or some of the other stuff in there yet.
• 02-08-2006
SlyMaelstrom
Consider using scanf() to scan in a float, assign the float to an integer and compare the two. If they subtract to 0 (or divide to 1), then the user typed an integer, if they don't, then it had a decimal in it.
• 02-08-2006
dwks
Code:

```                while (!scanf("%d", &menuChoice));                         {                                printf ("Enter a choice 1, 2, 3, 4,  5, or 6.\n");                         scanf ("%d", &menuChoice);                         }```
You have an extra semicolon there. This isn't a do-while loop.
• 02-08-2006
dwks
This is the program you're looking for from the FAQ (http://faq.cprogramming.com/cgi-bin/...043284385#opt2):
Code:

```#include <stdio.h> int main(void) {   int temp;     printf ("Input your number: ");   while (scanf("%d", &temp) != 1)   {     while (getchar() != '\n');     printf ("Try again: ");   }   printf ("You entered %d\n", temp);     return(0); }```
You might also want to add a clearerr into the loop:
Code:

```#include <stdio.h> int main(void) {   int temp;     printf ("Input your number: ");   while (scanf("%d", &temp) != 1)   {     clearerr(stdin);     while (getchar() != '\n');     printf ("Try again: ");   }   printf ("You entered %d\n", temp);     return(0); }```
• 02-08-2006
dwks
BTW, you can greatly reduce the calls to printf() by taking advantage of the fact that the preprocessor concatenates strings together:
Code:

```                printf ("Please enter a number from the menu.\n");                 printf ("1. Inches to centimeters\n");                 printf ("2. Feet to meters\n");                 printf ("3. Miles to kilometers\n");                 printf ("4. Pounds to kilograms\n");                 printf ("5. Gallons to liters\n");                 printf ("6. Quit the program\n");                 printf ("What Would You Like To Do?:");```
->
Code:

```                printf ("Please enter a number from the menu.\n"                     "1. Inches to centimeters\n"                     "2. Feet to meters\n"                     "3. Miles to kilometers\n"                     "4. Pounds to kilograms\n"                     "5. Gallons to liters\n"                     "6. Quit the program\n"                     "What Would You Like To Do?:");```
GCC complains if you make a string longer than 509 chars, though.
• 02-09-2006
jlharrison
Nice
Thanks again guys. I am learning this slowly but surley!
• 02-12-2006
Navar
Code:

```    int menuChoice;           do         {                 printf ("Please enter a number from the menu.\n");                 printf ("1. Inches to centimeters\n");                 printf ("2. Feet to meters\n");                 printf ("3. Miles to kilometers\n");                 printf ("4. Pounds to kilograms\n");                 printf ("5. Gallons to liters\n");                 printf ("6. Quit the program\n");                 printf ("What Would You Like To Do?:");                         scanf ("%d", &menuChoice);         if (menuChoice < 1 || menuChoice > 6)         {             printf("Please enter one of the given choices.\n");         }         } while ((menuChoice < 1) || (menuChoice > 6));```
this works...
• 02-12-2006
peterchen
try this one, it has the error checking i think you want (but it requires arrays and checking the array).

Code:

```#include <stdio.h> #include <ctype.h> int main(void) {     char ec[100];     int choice, i, error;         while(1) {         error = 0;                 printf ("Please enter a number from the menu.\n"         "1. Inches to centimeters\n"         "2. Feet to meters\n"         "3. Miles to kilometers\n"         "4. Pounds to kilograms\n"         "5. Gallons to liters\n"         "6. Quit the program\n"         "What Would You Like To Do?:");                 scanf("%s", ec);         getchar();                 // check for error in input         for(i=0;ec[i] !='\0';++i) {             if(!isdigit(ec[i])) {                 error = 1;                 break;             }         }                 choice = atoi(ec);                 if((choice > 0 && choice < 7) && !error) {             puts("input is correct");             break;         }         else puts("Error in input");     }         printf("You wanted choice %d\n", choice);     while(getchar()!=10);     return 0; }```
• 02-12-2006
dwks
Quote:

Code:

```    int menuChoice;           do         {                 printf ("Please enter a number from the menu.\n");                 printf ("1. Inches to centimeters\n");                 printf ("2. Feet to meters\n");                 printf ("3. Miles to kilometers\n");                 printf ("4. Pounds to kilograms\n");                 printf ("5. Gallons to liters\n");                 printf ("6. Quit the program\n");                 printf ("What Would You Like To Do?:");                         scanf ("%d", &menuChoice);         if (menuChoice < 1 || menuChoice > 6)         {             printf("Please enter one of the given choices.\n");         }         } while ((menuChoice < 1) || (menuChoice > 6));```
this works...
Try entering a letter and you'll see why it doesn't.

The best way to do it is to use the FAQ's program, as already mentioned:
Code:

```#include <stdio.h> int main(void) {   int temp;     printf ("Input your number: ");   while (scanf("%d", &temp) != 1)   {     while (getchar() != '\n');     printf ("Try again: ");   }   printf ("You entered %d\n", temp);     return(0); }```