Thread: goto command

  1. #1
    Registered User
    Join Date
    Feb 2006
    Posts
    6

    goto command

    I am having trouble with the goto command in the code below. The program should make two validation. First is if store selection is correct and second is if purcahse amount is numeric. If either is invalid the program should jump to ask the user if they want to try again. I have the first one working but if the user inputs an invalid sale amount(ie an alpha caracter), the program jumps to the terminate program line. Any suggestions?

    thanks

    Code:
    #include <stdio.h>
    
    main()
    {
    
    // variables
        int iResponse;
        int validate; 
        float purchaseAmount= 0.0;
        float delMarTaxRate=.0725;
        float encinitasTaxRate=.075;
        float laJollaTaxRate=.0775;
        float tax = 0.0;
        char option = 'Y';
    // Menu Selection
    
    	start:
        printf("\n\t\tStore Location\n");
        printf("\n\t1\tDel Mar\n");
        printf("\t2\tEncinitas\n");
        printf("\t3\tLa Jolla\n");
        printf("\nPlease select a location: ");
        scanf("%d", &iResponse);
        
    
    switch (iResponse) 
    	{
    
        	case 1:
            	printf("\nYou selected Del Mar\n\n");
            	break;
        	case 2:
            	printf("You selected Encinitas\n\n");
            	break;
        	case 3:
            	printf("You selected La Jolla\n\n");
            	break;
        	default:
            	printf("Invalid selection\n\n");
            	goto skip_point;
            
    	} //end switch
    
    // Input sale amount
    	printf("\tEnter Purchase Amount: ");
    
               { validate =scanf("%f", &purchaseAmount);
    		if (validate!=0);			
    		else goto skip_point;}
    // Display tax and total sale amount
    	
    	if(iResponse==1)
    	{	printf("\n\tThe tax amount for Del Mar is $%.2f\t", 
                		tax = (purchaseAmount*delMarTaxRate));
            	printf("\n\tThe total sale amount for Del Mar is $%.2f\t\n", 
                		purchaseAmount + (tax + .005));
           	}
             if(iResponse==2)
            {	printf("\n\tThe tax amount for Encinitas is $%.2f\t", 
                		tax =(purchaseAmount*encinitasTaxRate));
            	printf("\n\tThe total sale amount for Encinitas is $%.2f\t\n", 
                		purchaseAmount + tax );
           	}
            if(iResponse==3)
            {	printf("\n\tThe tax amount for La Jolla is $%.2f\t", 
                		tax = (purchaseAmount*laJollaTaxRate));                 
            	printf("\n\tThe total sale amount for La Jolla is $%.2f\t\n", 
                		purchaseAmount + (tax + .005));
            }
            		
    		
    		skip_point:
    			if (validate==0)
    				printf("\nInvalid purchase amount\n\n");
    				printf("\nTry again? Y/N\t");
    				scanf("%c",&option);
    								
    			while (option == 'y' || option == 'Y')
    				goto start;
    			if  (option =='n' || option == 'N')
    			 	goto end;
    			 	
    			 
    		
    		
    
            end:            
            printf("\nPress any key to terminate program");
            
    getch();   
      
    }

  2. #2
    Registered User
    Join Date
    Mar 2005
    Posts
    140
    The problem is a newline character is left in the input stream after each of your scanf calls.
    When you get here:
    Code:
    scanf("%c",&option);
    option is set to '\n' because the newline character is availible in the input buffer

    One solution would be to call getchar() after each scanf to clear the newline character.
    You can also search these boards for terms like flush input stream to read up on previous discussions... should also read the FAQ
    It's important to know how each input method handles whitespace and newlines.

    I'd also suggest you try to rewrite the program without using gotos.

    Your indentation needs some work.

    Code:
    if (validate==0)
    validate is uninitialized at this point if iResponse was invalid


    Code:
    			while (option == 'y' || option == 'Y')
    				goto start;
    			if  (option =='n' || option == 'N')
    			 	goto end;
    This 'while' can be an 'if', and there is no need for an else (goto end) since end is the next statement
    Last edited by spydoor; 02-21-2006 at 11:21 AM.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    >>I am having trouble with the goto command in the code below

    Stop doing that! Are are plenty of other structure programming techniques that eliminate the need to goto. Most people frown on its use. And the teachers I know will give you an F for turning in a program that uses it.
    Last edited by Ancient Dragon; 02-21-2006 at 11:45 AM.

  4. #4
    ex-DECcie
    Join Date
    Dec 2005
    Posts
    125
    Quote Originally Posted by Ancient Dragon
    >>I am having trouble with the goto command in the code below

    Stop doing that! Are are plenty of other structure programming techniques that eliminate the need to goto. Most people frown on its use. And the teachers I know will give you an F for turning in a program that uses it.

    I tend to agree, but like everything else, there are a few (and hopefully "far between") exceptions to this.

    I recall reading a paper some years back by the late W. Richard Stevens that advocated using goto in the implementation of some portion of TCP/IP that increased the performance of that particular part. He challenged the readers to find a better performing implementation that did not use goto. The details and the paper itself can probably still be found via Google.

    But, other than something like that, in a very low-level piece of code where it is used for very specific reasons, I too would echo the mantra: do not use goto.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 07-05-2010, 10:43 AM
  2. temperature sensors
    By danko in forum C Programming
    Replies: 22
    Last Post: 07-10-2007, 07:26 PM
  3. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  4. helpppp
    By The Brain in forum C Programming
    Replies: 1
    Last Post: 07-27-2005, 07:05 PM
  5. Goto Command
    By swgh in forum C++ Programming
    Replies: 13
    Last Post: 04-30-2005, 05:24 PM