Error checking issues

This is a discussion on Error checking issues within the C Programming forums, part of the General Programming Boards category; This program compiles, builds and runs; however, I am having 2 issues with the error checking in the program. The ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    7

    Error checking issues

    This program compiles, builds and runs; however, I am having 2 issues with the error checking in the program. The first issue is that when I choose case 4 to exit the program you get an error message to enter a valid choice; however, when you re-enter 4, the program decides to work and tells you to press enter to exit the program. The second issue is that when you choose a location and enter a valid numerical value the program continues; however, when you enter a letter or invalid input, the program goes into an endless loop. Any help would be greatly appreciated, thanks.

    The code is below:

    Code:
    #include <stdio.h>
    #include <system.h>
    #include <ctype.h>
    #include <stdlib.h>
    
    /*The main function*/
    void main()
    {
    
    char Purchase[32]; /* Declare variables */
    
    int i,Return=0;
    
    /*Variables for tax calculator*/
         int iResponse;
         #define DelMarRate 7.25 
         #define EncinitasRate 7.5
         #define LaJollaRate 7.75
         float DelMarTax = 0.0;
         float EncinitasTax = 0.0;
         float LaJollaTax = 0.0;
         float Amount = 0.0;
               
    /*Sales tax calculations for each of Kudler's locations*/
         
         DelMarTax = Amount * DelMarRate / 100;
         EncinitasTax = Amount * EncinitasRate / 100;
         LaJollaTax = Amount * LaJollaRate / 100;
        
    /*Menu to chose location*/
    	printf("\n*********************************");
       	printf("\n* Welcome to Kudler Fine foods! *");
       	printf("\n*********************************");
       	printf("\n\n");
    	
    	printf("\n1. Del Mar\n");
    	printf("\n2. Encinitas\n");
    	printf("\n3. La Jolla\n");
    	printf("\n4. End Program\n");
    	
    	printf("\nPlease Choose Kudler Location or 4 to exit: ");//Lets user know what tp input
    	
    	do {    /*Start loop*/  
     
    	scanf("%d", &iResponse);
    		if  (iResponse == 1 || iResponse <= 3) //Validates user input 
    		{
    		printf("\n\nPlease Enter the customer's purchase amount:$"); //display for user and prompt for input
    		}
    		else  	
    		{
    		printf("\nInvalid entry. Please select another option!\n");//Shows error and prompts user for a correct entry
    		}
    			
    	scanf("%f", &Amount);	
    	
         switch (iResponse){ /*Switch for case selection*/
       
    /*Displays cases for tax calculation output*/  
           case 1: /*Tax Calculation output for Del Mar*/
           
            for(i=0; Purchase[i]; i++) 
            {
                if(Purchase[i] < '0' || Purchase[i] > '9') /* Test for a numeric value (0-9) */
                {
                if (Purchase[i] == '.') /* If character is a decimal continue */
                continue;
                    else
                    Return=1; /* Set Non-Numerical Value flag */
                    break; 
                } 
            }
    
                if (Return == 1) /* If Non-Numerical Value print Error Message and allow user to re-enter purchase price */
                {
                    printf("\n\nError! Invalid Purchase Amount [ %s ]\n",Purchase);
                    fflush(stdin);
                    printf("\n\nPlease Re-enter Purchase Amount:$"); 
                    scanf("%s", Purchase);                
                } 
                
    	  DelMarTax = Amount * DelMarRate / 100;
              printf("\nDel Mar Tax on $%.2f = $%4.2f; Total = $%4.2f",Amount,DelMarTax,Amount+DelMarTax);
              printf("\n\n\n");                   
              printf("\nChoose another city or 4 to exit program: ");/*prompt user for input*/
              break;
          
           case 2: /*Tax Calculation output for Encinitis*/
           
              for(i=0; Purchase[i]; i++) 
            {
                if(Purchase[i] < '0' || Purchase[i] > '9') /* Test for a numeric value (0-9) */
                {
                if (Purchase[i] == '.') /* If character is a decimal continue */
                continue;
                    else
                    Return=1; /* Set Non-Numerical Value flag */
                    break; 
                } 
            }
    
                if (Return == 1) /* If Non-Numerical Value print Error Message and allow user to re-enter purchase price */
                {
                    printf("\n\nError! Invalid Purchase Amount [ %s ]\n",Purchase);
                    fflush(stdin);
                    printf("\n\nPlease Re-enter Purchase Amount:$"); 
                    scanf("%s", Purchase);                
                } 
                
    	  EncinitasTax = Amount * EncinitasRate / 100;
              printf("\nEncinitas Tax on $%.2f = $%4.2f; Total = $%4.2f",Amount,EncinitasTax,Amount+EncinitasTax); 
              printf("\n\n\n");       
              printf("\nChoose another city or 4 to exit program: ");/*prompt user for input*/
              break;
         
           case 3: /*Tax Calculation output for LaJolla*/
           
              for(i=0; Purchase[i]; i++) 
            {
                if(Purchase[i] < '0' || Purchase[i] > '9') /* Test for a numeric value (0-9) */
                {
                if (Purchase[i] == '.') /* If character is a decimal continue */
                continue;
                    else
                    Return=1; /* Set Non-Numerical Value flag */
                    break; 
                } 
            }
    
                if (Return == 1) /* If Non-Numerical Value print Error Message and allow user to re-enter purchase price */
                {
                    printf("\n\nError! Invalid Purchase Amount [ %s ]\n",Purchase);
                    fflush(stdin);
                    printf("\n\nPlease Re-enter Purchase Amount:$"); 
                    scanf("%s", Purchase);                
                } 
                
    	  LaJollaTax = Amount * LaJollaRate / 100;
              printf("\nLa Jolla Tax on $%.2f = $%4.2f; Total = $%4.2f",Amount,LaJollaTax,Amount+LaJollaTax); 
              printf("\n\n\n");      
              printf("\nChoose another city or 4 to exit program: ");/*prompt user for input*/
              break;           
     
           case 4:
           
            printf("\nThank You for using the Kudler Fine Foods Tax Calculator\n"); //ends program	
    	printf("\n\nPlease Press The Enter Key To Exit Program!\n\n");
    	} /*End Switch*/
    	}  while (iResponse!=4); /*End while loop*/
    
     scanf("%c\n");     
    }/*end main*/

  2. #2
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    944
    Code:
    (iResponse == 1 || iResponse <= 3)
    iResponse == 4 will make it go to the else part and say invalid entry. Should be iResponse <= 4

  3. #3
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    944
    Also, it looks like your program scans for the amount even if an invalid entry is pressed. You should have it keep prompting for the iResponse until it's a valid entry.

  4. #4
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    Okay, I changed
    Code:
     (iResponse == 1 || iResponse <= 3)
    to
    Code:
     (iResponse == 1 || iResponse <= 4)
    I still have to hit 4 2 times to exit the program as now I am asked to enter an amount when I hit 4 so this did not work. Also, I still have an endless loop if I enter an invalid entry in the amount of purchase for the location. Thanks.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    Finally, the program works good except for 1 last issue. The last issue is that, in the input checking for the calculation for each case, if I enter a non-numeric value, I get an endless loop, any suggestions. The code that gives me an endless loop if I enter a non-numeric value when the program asks for a purchase amount is:

    Code:
    case 1: /*Tax Calculation output for Del Mar*/
           
          do{
          Return = 0;
            /* Loop - Test the numeric values*/ 
            for(i=0; Purchase[i]; i++) 
            {
                if(Purchase[i] < '0' || Purchase[i] > '9') /* Test for a numeric value (0-9) */
                {
                if (Purchase[i] == '.') /* If character is a decimal continue */
                continue;
                    else
                    Return=1; /* Set Non-Numerical Value flag */
                    break; 
                } 
            }
    
                if (Return == 1) /* If Non-Numerical Value print Error Message and allow user to re-enter purchase price */
                {
                    printf("\n\nError! Invalid Purchase Amount [ %s ]\n",Purchase);
                    fflush(stdin);
                    printf("\n\nPlease Re-enter Purchase Amount:$"); 
                    scanf("%s", Purchase);                
                } 
                                 
    	}while (Return == 1);
    The complete program code is:

    Code:
    #include <stdio.h>
    #include <system.h>
    #include <ctype.h>
    #include <stdlib.h>
    
    /*The main function*/
    void main()
    {
    
    char Purchase[32]; /* Declare variables */
    
    int i,Return=0;
    
    /*Variables for tax calculator*/
         int iResponse;
         #define DelMarRate 7.25 
         #define EncinitasRate 7.5
         #define LaJollaRate 7.75
         float DelMarTax = 0.0;
         float EncinitasTax = 0.0;
         float LaJollaTax = 0.0;
         float Amount = 0.0;
               
    /*Sales tax calculations for each of Kudler's locations*/
         
         DelMarTax = Amount * DelMarRate / 100;
         EncinitasTax = Amount * EncinitasRate / 100;
         LaJollaTax = Amount * LaJollaRate / 100;
        
    /*Menu to chose location*/
    	printf("\n*********************************");
       	printf("\n* Welcome to Kudler Fine foods! *");
       	printf("\n*********************************");
       	printf("\n\n");
    	
    	printf("\n1. Del Mar\n");
    	printf("\n2. Encinitas\n");
    	printf("\n3. La Jolla\n");
    	printf("\n4. End Program\n");
    	
    	printf("\nPlease Choose Kudler Location or 4 to exit: "); /*requests user input*/
    	do {    /*Start loop*/  
     
    	scanf("%d", &iResponse);
            if  (iResponse == 1 || iResponse <= 4) /*validates user input*/
            { 
              if(iResponse == 1 || iResponse <= 3)
                  {
                       printf("\n\nPlease Enter the customer's purchase amount:$");
                       scanf("%f", &Amount);	
                   }
            }
            else     
            {
            printf("\nInvalid entry. Please select another option!\n");/*shows error*/
            }
    	
         switch (iResponse){ /*Switch for case selection*/
       
    	/*Displays cases for tax calculation output*/  
          case 1: /*Tax Calculation output for Del Mar*/
           
          do{
          Return = 0;
            /* Loop - Test the numeric values*/ 
            for(i=0; Purchase[i]; i++) 
            {
                if(Purchase[i] < '0' || Purchase[i] > '9') /* Test for a numeric value (0-9) */
                {
                if (Purchase[i] == '.') /* If character is a decimal continue */
                continue;
                    else
                    Return=1; /* Set Non-Numerical Value flag */
                    break; 
                } 
            }
    
                if (Return == 1) /* If Non-Numerical Value print Error Message and allow user to re-enter purchase price */
                {
                    printf("\n\nError! Invalid Purchase Amount [ %s ]\n",Purchase);
                    fflush(stdin);
                    printf("\n\nPlease Re-enter Purchase Amount:$"); 
                    scanf("%s", Purchase);                
                } 
                                 
    	}while (Return == 1);
                
    	  DelMarTax = Amount * DelMarRate / 100;
    	  
              printf("*************************************************\n");
              printf("\nDel Mar Tax on $%.2f = $%4.2f; Total = $%4.2f",Amount,DelMarTax,Amount+DelMarTax);
              printf("\n\n\n");                   
              printf("\nChoose another location or 4 to exit program: ");/*prompt user for input*/
              break;
          
           case 2: /*Tax Calculation output for Encinitis*/
           
           do{
           Return = 0;
           
            /* Loop - Test the numeric values*/ 
            for(i=0; Purchase[i]; i++) 
            {
                if(Purchase[i] < '0' || Purchase[i] > '9') /* Test for a numeric value (0-9) */
                {
                if (Purchase[i] == '.') /* If character is a decimal continue */
                continue;
                    else
                    Return=1; /* Set Non-Numerical Value flag */
                    break; 
                } 
            }
    
                if (Return == 1) /* If Non-Numerical Value print Error Message and allow user to re-enter purchase price */
                {
                    printf("\n\nError! Invalid Purchase Amount [ %s ]\n",Purchase);
                    fflush(stdin);
                    printf("\n\nPlease Re-enter Purchase Amount:$"); 
                    scanf("%s", Purchase);                
                } 
                                 
    	}while (Return == 1);
                
    	  EncinitasTax = Amount * EncinitasRate / 100;
    	  
    	  printf("*************************************************\n");
              printf("\nEncinitas Tax on $%.2f = $%4.2f; Total = $%4.2f",Amount,EncinitasTax,Amount+EncinitasTax); 
              printf("\n\n\n");       
              printf("\nChoose another location or 4 to exit program: ");/*prompt user for input*/
              break;
         
           case 3: /*Tax Calculation output for LaJolla*/
           
           do{
           Return = 0;
           
            /* Loop - Test the numeric values*/ 
            for(i=0; Purchase[i]; i++) 
            {
                if(Purchase[i] < '0' || Purchase[i] > '9') /* Test for a numeric value (0-9) */
                {
                if (Purchase[i] == '.') /* If character is a decimal continue */
                continue;
                    else
                    Return=1; /* Set Non-Numerical Value flag */
                    break; 
                } 
            }
    
                if (Return == 1) /* If Non-Numerical Value print Error Message and allow user to re-enter purchase price */
                {
                    printf("\n\nError! Invalid Purchase Amount [ %s ]\n",Purchase);
                    fflush(stdin);
                    printf("\n\nPlease Re-enter Purchase Amount:$"); 
                    scanf("%s", Purchase);                
                } 
                                 
            }while (Return == 1);
    
    	  LaJollaTax = Amount * LaJollaRate / 100;
    	  
    	  printf("*************************************************\n");
              printf("\nLa Jolla Tax on $%.2f = $%4.2f; Total = $%4.2f",Amount,LaJollaTax,Amount+LaJollaTax); 
              printf("\n\n\n");      
              printf("\nChoose another location or 4 to exit program: ");/*prompt user for input*/
              break;           
     
           case 4:
           
            printf("\nThank You for using the Kudler Fine Foods Tax Calculator\n");   
        	printf("\n\nPress The Enter Key To Exit Program!\n\n");
    
        	
    	break;
    	default:
    	printf("\n\nPress 4 to Exit Program!\n\n");
    
    	} /*End Switch*/
    	}  while (iResponse!=4); /*End while loop*/
    
     scanf("%c\n");
          
    }/*end main*/
    The program works great as long as a non-numeric value is not entered after the user enters a location option and the program asks for a purchase amount. Thanks a lot for all your help everyone.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Check the return value of scanf() for the numeric value. If zero, suck a character out of the buffer and try again.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    Registered User
    Join Date
    Oct 2009
    Posts
    7
    how do I check the return value of scanf(), thanks.

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    int return_value = scanf(......) ;
    if (return_value == ......) { ... }
    else { .... }
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Serious template issues (!!!)
    By Mikal in forum C++ Programming
    Replies: 2
    Last Post: 07-23-2009, 08:46 AM
  2. issues
    By OrAnGeWorX in forum C Programming
    Replies: 35
    Last Post: 11-18-2008, 11:18 PM
  3. Bitmap scroll issues
    By Gerread in forum Windows Programming
    Replies: 4
    Last Post: 05-14-2007, 05:18 AM
  4. Directx issues
    By dpro in forum Game Programming
    Replies: 7
    Last Post: 03-30-2005, 12:58 PM
  5. hexdump issues
    By daluu in forum C Programming
    Replies: 2
    Last Post: 03-04-2003, 08:01 PM

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