I am new to C programming and have been using the GOTO all over the place until today when I was informed it is not preferred practice, and due to its adverse affects on debugging and maintenance should only be used in the rarest of cases.

I can understand that I guess,.

Would an "acceptable" use of GOTO be to rerun the entire main loop? If I make it all the way through my logic, and simply want to start over at the top, would not a simple GOTO back to the top be the best option?

Try to avoid GOTO. Put main loop inside one more loop or try some other logic. Use of GOTO will create all the mess at last.

commented: Absolutely yes. +36

Would an "acceptable" use of GOTO be to rerun the entire main loop?

No.

If I make it all the way through my logic, and simply want to start over at the top, would not a simple GOTO back to the top be the best option?

Try to think about the real loop control and a better loop construct will come to mind.
---
http://en.wikipedia.org/wiki/Goto#Criticism_of_goto_usage

commented: nice reference. +2
commented: helpful +3

goto makes the code unreadable and hard to debug. The only place where goto is advisable is coming out of a nested loop.

No.
Try to think about the real loop control and a better loop construct will come to mind.
---
http://en.wikipedia.org/wiki/Goto#Criticism_of_goto_usage

This suggestion intrigues me. Putting my main loop inside another loop. I am not sure I understand how to do it, and it sounds almost more complicated than the problems it might potentially avoid, but if anyone can expand on this thought I would appreciate it.
Again, my limited experience hinders my understanding at this point.

This suggestion intrigues me. Putting my main loop inside another loop. I am not sure I understand how to do it, and it sounds almost more complicated than the problems it might potentially avoid, but if anyone can expand on this thought I would appreciate it.
Again, my limited experience hinders my understanding at this point.

Perhaps if you provide a code snippet on where you think the goto should be used, you may get responses that satisfy your needs.

I'm not totally against the goto, but I haven't used one - well, I can't remember when. Up to about 5 years ago, a language I use on the System i (aka AS400) known as CL had very few good looping constructs (actually none) and your only choice was to use a goto.

Most languages these days (decent ones) provide adequate constructs with regards to sequence, iterations and decisions that would really make you think long and hard as to why you would need to use a goto.

9868 makes a valid observation regarding breaking out of a nested loop, but it would have to be a really deeply nested loop and if that's the case, then it's more than likely a design issue. However, "modern" languages usually provide a "psuedo" goto (e.g. Java with break and a label) for these situations.

Cheers,
JD

This suggestion intrigues me. Putting my main loop inside another loop. I am not sure I understand how to do it, and it sounds almost more complicated than the problems it might potentially avoid, but if anyone can expand on this thought I would appreciate it.
Again, my limited experience hinders my understanding at this point.

In this post, what is it that breaks the loop -- entering an 'n'? How easy is it to spot loop conditions? Might it be easier to tell if it were something like this?

do {
      /* show menu */
      /* take user input */
      /* show result or error */
      /* prompt to repeat */
   } while ( toupper(cAgain) != 'N' );

Here is the type of program I am talking about. It is not a nested loop or anything like that, but once I get to the end, if they user enters Y I would like to send them back to the top and start all over.
I am still not even sure how I would do this with the loop-inside-loop suggestion.

#include <stdio.h>  // standard input output library

//defines tax value for calculations
#define DelMar 7.25
#define Encinitas 7.5
#define LaJolla 7.75

char cAgain;  //variable for Y/N questions

float user_input() // week 4 addition for sales amount
{ //prompt, collect, and store sale amount
    float fAmount; //defines amount type as float
    printf("Enter Amount of Sale $ "); 
    scanf ("%f", &fAmount);
    return fAmount;
}

int iMenu()  //defines menu variable
{ // menu which prompts, collects, and stores store number
    int iSelection; //defines selection type as integer
    printf("Tax Calculation for Purchase\n\n"); //changed to reflect week 4 updates
    printf("1. Del Mar \n2. Encinitas \n3. La Jolla\n");
    printf("\n\nPlease Select Number of Store for Which to Calculate: ");

    scanf("%d",&iSelection);
    return iSelection;
}

int main() //main loop
{
STARTOVER://start again point
{				

    float fSales = 0.00; // Sales value for calculations
    int iStorenum; //defines store for main loop, menu will not work without
      
    fSales = user_input(); //new week 4 loop for sales total
    while (fSales <0.0) //new week 4 loop to validate sales amount
    {
    printf("SALE AMOUNT CANNOT BE LESS THAN $0.00. Re-Enter: $ ");
        {
        scanf ("%f",&fSales); // scans new input for validity
        }
         
     } // ends while loop
     
    iStorenum = iMenu(); //displays menu created above
    
     while (iStorenum <1 || iStorenum >3) //if store number is not 1, 2 or 3
    {
    	printf("INVALID NUMBER ENTERED! Please enter 1,2 or 3: ");
    	   {
    	   scanf("%d", &iStorenum);  //scans input at INVALID prompt 
           }
    } // end while loop
    
    if (iStorenum == 1)
//Calculates and Displays Del Mar Store, Sales, Tax rate, and Tax
       printf("\nDel Mar \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\tTotal= \t$%.2f\t\n\n",fSales, DelMar, fSales*DelMar/100, DelMar*fSales/100+fSales); 
    if (iStorenum == 2)
//Calculates and Displays Encinitas Store, Sales, Tax rate, and Tax
       printf("\nEncinitas \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\tTotal= \t$%.2f\t\n\n",fSales, Encinitas, fSales*Encinitas/100, Encinitas*fSales/100+fSales); 
    if (iStorenum == 3)
 //Calculates and Displays La Jolla Store, Sales, Tax rate, and Tax
       printf("\nLa Jolla \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\tTotal= \t$%.2f\t\n\n",fSales, LaJolla, fSales*LaJolla/100, LaJolla*fSales/100+fSales); 
                 
   
    

    printf("\n\tWould you like to calculate for another store? (y/n) ");//prompts for repeat
            scanf("%c", &cAgain);//scans input at store number prompt   
            
               if (cAgain == 'y'|| cAgain == 'Y')//repeats calculator
               {
               goto STARTOVER;  //back to top
               }
               else if (cAgain == 'n' || cAgain == 'N')//end
                            
  return 0; // successful loop end
}
}

You would use a do-while loop. Essentially you'll keep asking for user input in the body of the do loop while the user keeps answering 'y' or 'Y' for more calculations.

int main() //main loop
{

    do {

        float fSales = 0.00; // Sales value for calculations
        int iStorenum; //defines store for main loop, menu will not work without

        fSales = user_input(); //new week 4 loop for sales total
        while (fSales < 0.0) //new week 4 loop to validate sales amount
        {
            printf("SALE AMOUNT CANNOT BE LESS THAN $0.00. Re-Enter: $ ");
            {
                scanf("%f", &fSales); // scans new input for validity
            }

        } // ends while loop

        iStorenum = iMenu(); //displays menu created above

        while (iStorenum < 1 || iStorenum > 3) //if store number is not 1, 2 or 3
        {
            printf("INVALID NUMBER ENTERED! Please enter 1,2 or 3: ");
            {
                scanf("%d", &iStorenum); //scans input at INVALID prompt
            }
        } // end while loop

        if (iStorenum == 1)
            //Calculates and Displays Del Mar Store, Sales, Tax rate, and Tax
            printf(
                    "\nDel Mar \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\tTotal= \t$%.2f\t\n\n",
                    fSales, DelMar, fSales * DelMar / 100, DelMar * fSales
                            / 100 + fSales);
        if (iStorenum == 2)
            //Calculates and Displays Encinitas Store, Sales, Tax rate, and Tax
            printf(
                    "\nEncinitas \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\tTotal= \t$%.2f\t\n\n",
                    fSales, Encinitas, fSales * Encinitas / 100, Encinitas
                            * fSales / 100 + fSales);
        if (iStorenum == 3)
            //Calculates and Displays La Jolla Store, Sales, Tax rate, and Tax
            printf(
                    "\nLa Jolla \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\tTotal= \t$%.2f\t\n\n",
                    fSales, LaJolla, fSales * LaJolla / 100, LaJolla * fSales
                            / 100 + fSales);

        printf("\n\tWould you like to calculate for another store? (y/n) ");//prompts for repeat
        scanf("%c", &cAgain);//scans input at store number prompt

    } while (cAgain == 'y' || cAgain == 'Y');

    return 0;
}

By the way, your code doesn't work quite right at the moment as it only prompts for one set of input data. When prompting for the answer to more calculations, the scanf will pick up the '\n' character from the previous scanf of the store number. You'll have to "consume" that character from the input buffer before prompting for character input.

Thanks Yellow Snow, I am going to give that a try.
Can you help me on something stupid real quick? I have two copies of my code saved, one works and one does not (as you pointed out). For the life of me I cannot see the difference.
Can you look as this working version and tell me what is different?
I have gone through line by line and both versions look identical to me.

#include <stdio.h>  // standard input output library

//defines tax value for calculations
#define DelMar 7.25
#define Encinitas 7.5
#define LaJolla 7.75

char cAgain;  //variable for Y/N questions

float user_input() // week 4 addition for sales amount
{ //prompt, collect, and store sale amount
    float fAmount; //defines amount type as float
    printf("Enter Amount of Sale $ "); 
    scanf ("%f", &fAmount);
    return fAmount;
}

int iMenu()  //defines menu variable
{ // menu which prompts, collects, and stores store number
    int iSelection; //defines selection type as integer
    printf("Tax Calculation for Purchase\n\n"); //changed to reflect week 4 updates
    printf("1. Del Mar \n2. Encinitas \n3. La Jolla\n");
    printf("\n\nPlease Select Number of Store for Which to Calculate: ");

    scanf("%d",&iSelection);
    return iSelection;
}

int main() //main loop
{
STARTOVER://start again point
{				

    float fSales = 0.00; // Sales value for calculations
    int iStorenum; //defines store for main loop, menu will not work without
      
    fSales = user_input(); //new week 4 loop for sales total
    while (fSales <0.0) //new week 4 loop to validate sales amount
    {
    printf("SALE AMOUNT CANNOT BE LESS THAN $0.00. Re-Enter: $ ");
        {
        scanf ("%f",&fSales); // scans new input for validity
        }
         
     } // ends while loop
     
    iStorenum = iMenu(); //displays menu created above
    
     while (iStorenum <1 || iStorenum >3) //if store number is not 1, 2 or 3
    {
    	printf("INVALID NUMBER ENTERED! Please enter 1,2 or 3: ");
    	   {
    	   scanf("%d", &iStorenum);  //scans input at INVALID prompt 
           }
    } // end while loop
    
    if (iStorenum == 1)
//Calculates and Displays Del Mar Store, Sales, Tax rate, and Tax
       printf("\nDel Mar \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\tTotal= \t$%.2f\t\n\n",fSales, DelMar, fSales*DelMar/100, DelMar*fSales/100+fSales); 
    if (iStorenum == 2)
//Calculates and Displays Encinitas Store, Sales, Tax rate, and Tax
       printf("\nEncinitas \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\tTotal= \t$%.2f\t\n\n",fSales, Encinitas, fSales*Encinitas/100, Encinitas*fSales/100+fSales); 
    if (iStorenum == 3)
 //Calculates and Displays La Jolla Store, Sales, Tax rate, and Tax
       printf("\nLa Jolla \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\tTotal= \t$%.2f\t\n\n",fSales, LaJolla, fSales*LaJolla/100, LaJolla*fSales/100+fSales); 
                 
   
    

    printf("\n\tRun Calculator Again? (y/n) ");//prompts for repeat
            scanf("%c", &cAgain);//scans input at store number prompt   
            
               if (cAgain == 'y'|| cAgain == 'Y')//repeats calculator
               {
               goto STARTOVER;  //back to top
               }
               else if (cAgain == 'n' || cAgain == 'N')//end
                            
  return 0; // successful loop end
}
}

Rather than "eye balling" the two files and trying to detect the differences, use a file compare utility that is provided with your OS e.g. fc on Windows or diff on Linux/Unix.

commented: good point +3
commented: super helpful +3

never knew such a thing existed. I will give it a try.
Thanks again for all your help.

Thanks for you all, now i noe how to use GOTO liao...
haha^^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.