| | |
GOTO command
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
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?
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?
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
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.
Freedom in the Mind, Faith in the words.. Pride in our Souls...
Indian Developer
http://falaque.wordpress.com/
Indian Developer
http://falaque.wordpress.com/
•
•
•
•
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?
---
http://en.wikipedia.org/wiki/Goto#Cr..._of_goto_usage
Last edited by Dave Sinkula; Aug 7th, 2009 at 1:34 am.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
•
•
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
Again, my limited experience hinders my understanding at this point.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
•
•
•
•
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.
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.
Manic twiddler of bits
•
•
•
•
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.
C Syntax (Toggle Plain Text)
do { /* show menu */ /* take user input */ /* show result or error */ /* prompt to repeat */ } while ( toupper(cAgain) != 'N' );
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
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.
I am still not even sure how I would do this with the loop-inside-loop suggestion.
C Syntax (Toggle Plain Text)
#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 } }
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
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.
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.
C Syntax (Toggle Plain Text)
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; }
Manic twiddler of bits
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.
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.
C Syntax (Toggle Plain Text)
#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 } }
Last edited by no1zson; Aug 9th, 2009 at 12:53 am.
I never drew first, but I drew first blood.
I'm no ones son, unforgiven.
I'm no ones son, unforgiven.
![]() |
Similar Threads
- Python goto (Python)
- Problem with Netgear Firewall (Windows NT / 2000 / XP)
- How to use the goto command? (Pascal and Delphi)
- Completed Code but A LOT of GOTO...Replacement? (C++)
- Problem with variables in Windows shell script (Windows NT / 2000 / XP)
- Need Help! Novice of ASP.NET (ASP.NET)
- links not working in IE6 (Web Browsers)
Other Threads in the C Forum
- Previous Thread: Have a look...
- Next Thread: My Assignment...Hu can help me???
| Thread Tools | Search this Thread |
adobe ansi api array arrays asterisks binarysearch calculate centimeter char convert copyanyfile copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory dynamic fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop initialization interest kernel km linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix power probleminc program programming pyramidusingturboccodes radix read recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming stack standard string strings structures systemcall testautomation turboc unix user variable voidmain() wab win32api windows.h






