Error Checking

This is a discussion on Error Checking within the C Programming forums, part of the General Programming Boards category; I need to make sure the user enters a number from the menu. The do while loop will ensure that ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    36

    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

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Jan 2006
    Posts
    36
    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.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,066
    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.
    Sent from my iPadŽ

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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);
    }
    Last edited by dwks; 02-08-2006 at 04:58 PM. Reason: Added FAQ link
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Jan 2006
    Posts
    36

    Nice

    Thanks again guys. I am learning this slowly but surley!

  9. #9
    Registered User
    Join Date
    Jan 2006
    Posts
    10
    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...

  10. #10
    Registered User
    Join Date
    Dec 2005
    Location
    Australia - Melbourne
    Posts
    63
    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;
    }
    Last edited by peterchen; 02-12-2006 at 06:38 AM.

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    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);
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21