943,866 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 1182
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Aug 7th, 2009
0

GOTO command

Expand Post »
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?
Similar Threads
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Aug 7th, 2009
1

Re: GOTO command

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.
Reputation Points: 165
Solved Threads: 59
Posting Pro in Training
DangerDev is offline Offline
485 posts
since Jan 2008
Aug 7th, 2009
2

Re: GOTO command

Click to Expand / Collapse  Quote originally posted by no1zson ...
Would an "acceptable" use of GOTO be to rerun the entire main loop?
No.
Click to Expand / Collapse  Quote originally posted by no1zson ...
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#Cr..._of_goto_usage
Last edited by Dave Sinkula; Aug 7th, 2009 at 1:34 am.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 7th, 2009
0

Re: GOTO command

goto makes the code unreadable and hard to debug. The only place where goto is advisable is coming out of a nested loop.
Reputation Points: 45
Solved Threads: 7
Light Poster
9868 is offline Offline
36 posts
since Mar 2009
Aug 7th, 2009
0

Re: GOTO command

No.
Try to think about the real loop control and a better loop construct will come to mind.
---
http://en.wikipedia.org/wiki/Goto#Cr..._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.
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Aug 7th, 2009
0

Re: GOTO command

Click to Expand / Collapse  Quote originally posted by no1zson ...
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
Last edited by yellowSnow; Aug 7th, 2009 at 2:10 pm.
Reputation Points: 651
Solved Threads: 35
Posting Whiz in Training
yellowSnow is offline Offline
201 posts
since Jul 2009
Aug 7th, 2009
0

Re: GOTO command

Click to Expand / Collapse  Quote originally posted by no1zson ...
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?
  1. do {
  2. /* show menu */
  3. /* take user input */
  4. /* show result or error */
  5. /* prompt to repeat */
  6. } while ( toupper(cAgain) != 'N' );
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 7th, 2009
0

Re: GOTO command

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.
  1.  
  2. #include <stdio.h> // standard input output library
  3.  
  4. //defines tax value for calculations
  5. #define DelMar 7.25
  6. #define Encinitas 7.5
  7. #define LaJolla 7.75
  8.  
  9. char cAgain; //variable for Y/N questions
  10.  
  11. float user_input() // week 4 addition for sales amount
  12. { //prompt, collect, and store sale amount
  13. float fAmount; //defines amount type as float
  14. printf("Enter Amount of Sale $ ");
  15. scanf ("%f", &fAmount);
  16. return fAmount;
  17. }
  18.  
  19. int iMenu() //defines menu variable
  20. { // menu which prompts, collects, and stores store number
  21. int iSelection; //defines selection type as integer
  22. printf("Tax Calculation for Purchase\n\n"); //changed to reflect week 4 updates
  23. printf("1. Del Mar \n2. Encinitas \n3. La Jolla\n");
  24. printf("\n\nPlease Select Number of Store for Which to Calculate: ");
  25.  
  26. scanf("%d",&iSelection);
  27. return iSelection;
  28. }
  29.  
  30. int main() //main loop
  31. {
  32. STARTOVER://start again point
  33. {
  34.  
  35. float fSales = 0.00; // Sales value for calculations
  36. int iStorenum; //defines store for main loop, menu will not work without
  37.  
  38. fSales = user_input(); //new week 4 loop for sales total
  39. while (fSales <0.0) //new week 4 loop to validate sales amount
  40. {
  41. printf("SALE AMOUNT CANNOT BE LESS THAN $0.00. Re-Enter: $ ");
  42. {
  43. scanf ("%f",&fSales); // scans new input for validity
  44. }
  45.  
  46. } // ends while loop
  47.  
  48. iStorenum = iMenu(); //displays menu created above
  49.  
  50. while (iStorenum <1 || iStorenum >3) //if store number is not 1, 2 or 3
  51. {
  52. printf("INVALID NUMBER ENTERED! Please enter 1,2 or 3: ");
  53. {
  54. scanf("%d", &iStorenum); //scans input at INVALID prompt
  55. }
  56. } // end while loop
  57.  
  58. if (iStorenum == 1)
  59. //Calculates and Displays Del Mar Store, Sales, Tax rate, and Tax
  60. printf("\nDel Mar \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\tTotal= \t$%.2f\t\n\n",fSales, DelMar, fSales*DelMar/100, DelMar*fSales/100+fSales);
  61. if (iStorenum == 2)
  62. //Calculates and Displays Encinitas Store, Sales, Tax rate, and Tax
  63. printf("\nEncinitas \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\tTotal= \t$%.2f\t\n\n",fSales, Encinitas, fSales*Encinitas/100, Encinitas*fSales/100+fSales);
  64. if (iStorenum == 3)
  65. //Calculates and Displays La Jolla Store, Sales, Tax rate, and Tax
  66. printf("\nLa Jolla \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\tTotal= \t$%.2f\t\n\n",fSales, LaJolla, fSales*LaJolla/100, LaJolla*fSales/100+fSales);
  67.  
  68.  
  69.  
  70.  
  71. printf("\n\tWould you like to calculate for another store? (y/n) ");//prompts for repeat
  72. scanf("%c", &cAgain);//scans input at store number prompt
  73.  
  74. if (cAgain == 'y'|| cAgain == 'Y')//repeats calculator
  75. {
  76. goto STARTOVER; //back to top
  77. }
  78. else if (cAgain == 'n' || cAgain == 'N')//end
  79.  
  80. return 0; // successful loop end
  81. }
  82. }
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007
Aug 8th, 2009
0

Re: GOTO command

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.
  1. int main() //main loop
  2. {
  3.  
  4. do {
  5.  
  6. float fSales = 0.00; // Sales value for calculations
  7. int iStorenum; //defines store for main loop, menu will not work without
  8.  
  9. fSales = user_input(); //new week 4 loop for sales total
  10. while (fSales < 0.0) //new week 4 loop to validate sales amount
  11. {
  12. printf("SALE AMOUNT CANNOT BE LESS THAN $0.00. Re-Enter: $ ");
  13. {
  14. scanf("%f", &fSales); // scans new input for validity
  15. }
  16.  
  17. } // ends while loop
  18.  
  19. iStorenum = iMenu(); //displays menu created above
  20.  
  21. while (iStorenum < 1 || iStorenum > 3) //if store number is not 1, 2 or 3
  22. {
  23. printf("INVALID NUMBER ENTERED! Please enter 1,2 or 3: ");
  24. {
  25. scanf("%d", &iStorenum); //scans input at INVALID prompt
  26. }
  27. } // end while loop
  28.  
  29. if (iStorenum == 1)
  30. //Calculates and Displays Del Mar Store, Sales, Tax rate, and Tax
  31. printf(
  32. "\nDel Mar \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\tTotal= \t$%.2f\t\n\n",
  33. fSales, DelMar, fSales * DelMar / 100, DelMar * fSales
  34. / 100 + fSales);
  35. if (iStorenum == 2)
  36. //Calculates and Displays Encinitas Store, Sales, Tax rate, and Tax
  37. printf(
  38. "\nEncinitas \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\tTotal= \t$%.2f\t\n\n",
  39. fSales, Encinitas, fSales * Encinitas / 100, Encinitas
  40. * fSales / 100 + fSales);
  41. if (iStorenum == 3)
  42. //Calculates and Displays La Jolla Store, Sales, Tax rate, and Tax
  43. printf(
  44. "\nLa Jolla \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\tTotal= \t$%.2f\t\n\n",
  45. fSales, LaJolla, fSales * LaJolla / 100, LaJolla * fSales
  46. / 100 + fSales);
  47.  
  48. printf("\n\tWould you like to calculate for another store? (y/n) ");//prompts for repeat
  49. scanf("%c", &cAgain);//scans input at store number prompt
  50.  
  51. } while (cAgain == 'y' || cAgain == 'Y');
  52.  
  53. return 0;
  54. }
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.
Reputation Points: 651
Solved Threads: 35
Posting Whiz in Training
yellowSnow is offline Offline
201 posts
since Jul 2009
Aug 9th, 2009
0

Re: GOTO command

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.
  1. #include <stdio.h> // standard input output library
  2.  
  3. //defines tax value for calculations
  4. #define DelMar 7.25
  5. #define Encinitas 7.5
  6. #define LaJolla 7.75
  7.  
  8. char cAgain; //variable for Y/N questions
  9.  
  10. float user_input() // week 4 addition for sales amount
  11. { //prompt, collect, and store sale amount
  12. float fAmount; //defines amount type as float
  13. printf("Enter Amount of Sale $ ");
  14. scanf ("%f", &fAmount);
  15. return fAmount;
  16. }
  17.  
  18. int iMenu() //defines menu variable
  19. { // menu which prompts, collects, and stores store number
  20. int iSelection; //defines selection type as integer
  21. printf("Tax Calculation for Purchase\n\n"); //changed to reflect week 4 updates
  22. printf("1. Del Mar \n2. Encinitas \n3. La Jolla\n");
  23. printf("\n\nPlease Select Number of Store for Which to Calculate: ");
  24.  
  25. scanf("%d",&iSelection);
  26. return iSelection;
  27. }
  28.  
  29. int main() //main loop
  30. {
  31. STARTOVER://start again point
  32. {
  33.  
  34. float fSales = 0.00; // Sales value for calculations
  35. int iStorenum; //defines store for main loop, menu will not work without
  36.  
  37. fSales = user_input(); //new week 4 loop for sales total
  38. while (fSales <0.0) //new week 4 loop to validate sales amount
  39. {
  40. printf("SALE AMOUNT CANNOT BE LESS THAN $0.00. Re-Enter: $ ");
  41. {
  42. scanf ("%f",&fSales); // scans new input for validity
  43. }
  44.  
  45. } // ends while loop
  46.  
  47. iStorenum = iMenu(); //displays menu created above
  48.  
  49. while (iStorenum <1 || iStorenum >3) //if store number is not 1, 2 or 3
  50. {
  51. printf("INVALID NUMBER ENTERED! Please enter 1,2 or 3: ");
  52. {
  53. scanf("%d", &iStorenum); //scans input at INVALID prompt
  54. }
  55. } // end while loop
  56.  
  57. if (iStorenum == 1)
  58. //Calculates and Displays Del Mar Store, Sales, Tax rate, and Tax
  59. printf("\nDel Mar \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\tTotal= \t$%.2f\t\n\n",fSales, DelMar, fSales*DelMar/100, DelMar*fSales/100+fSales);
  60. if (iStorenum == 2)
  61. //Calculates and Displays Encinitas Store, Sales, Tax rate, and Tax
  62. printf("\nEncinitas \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\tTotal= \t$%.2f\t\n\n",fSales, Encinitas, fSales*Encinitas/100, Encinitas*fSales/100+fSales);
  63. if (iStorenum == 3)
  64. //Calculates and Displays La Jolla Store, Sales, Tax rate, and Tax
  65. printf("\nLa Jolla \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\tTotal= \t$%.2f\t\n\n",fSales, LaJolla, fSales*LaJolla/100, LaJolla*fSales/100+fSales);
  66.  
  67.  
  68.  
  69.  
  70. printf("\n\tRun Calculator Again? (y/n) ");//prompts for repeat
  71. scanf("%c", &cAgain);//scans input at store number prompt
  72.  
  73. if (cAgain == 'y'|| cAgain == 'Y')//repeats calculator
  74. {
  75. goto STARTOVER; //back to top
  76. }
  77. else if (cAgain == 'n' || cAgain == 'N')//end
  78.  
  79. return 0; // successful loop end
  80. }
  81. }
Last edited by no1zson; Aug 9th, 2009 at 12:53 am.
Reputation Points: 59
Solved Threads: 1
Posting Whiz in Training
no1zson is offline Offline
226 posts
since Jul 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Have a look...
Next Thread in C Forum Timeline: Help with C programming





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC