How To Return to Main()

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2008
Posts: 53
Reputation: arshad115 is an unknown quantity at this point 
Solved Threads: 2
arshad115's Avatar
arshad115 arshad115 is offline Offline
Junior Poster in Training

How To Return to Main()

 
0
  #1
Mar 10th, 2009
i have so many functions in my program,what i want to do is, i want to return back to the main() but in somewhere middle of it!.

should i use goto statements?
i tried 'em,it gave some error!

if i should use goto then please explain how to,thanks
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: How To Return to Main()

 
0
  #2
Mar 10th, 2009
I'm guessing you're a newbie.

There are a few justifications that warrant the use of gotos. I'm willing to bet this ain't one of them.

Post your code?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: How To Return to Main()

 
0
  #3
Mar 10th, 2009
It's freeware in the public domain. Try this, it works:
  1. struct Cry {
  2. const char* what() const {
  3. return "Mummy, I\'m afraid!";
  4. }
  5. };
  6. void Lots() { 2*2==4; throw Cry(); }
  7. void of () { Lots(); }
  8. void functions () { of(); }
  9. int main()
  10. {
  11. try {
  12. functions();
  13. }
  14. catch(Cry mail) {
  15. cout << mail.what() << "..\n";
  16. cout << "I\'m very glad to see you!" << endl;
  17. }
  18. catch(...) {
  19. cout << "Bye forever ;(" << endl;
  20. }
  21. return 0;
  22. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,266
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert

Re: How To Return to Main()

 
0
  #4
Mar 10th, 2009
The above is proof that people who use c++ are geeks and have no life
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 53
Reputation: arshad115 is an unknown quantity at this point 
Solved Threads: 2
arshad115's Avatar
arshad115 arshad115 is offline Offline
Junior Poster in Training

Re: How To Return to Main()

 
0
  #5
Mar 10th, 2009
ya i am a newbie!
i want to return to the lines above ,where i call the function,i cant design my program.
i want to return to a menu which is in the main and if the user wants to run the function again then he can select it again from the main

Originally Posted by ArkM View Post
It's freeware in the public domain. Try this, it works:
  1. struct Cry {
  2. const char* what() const {
  3. return "Mummy, I\'m afraid!";
  4. }
  5. };
  6. void Lots() { 2*2==4; throw Cry(); }
  7. void of () { Lots(); }
  8. void functions () { of(); }
  9. int main()
  10. {
  11. try {
  12. functions();
  13. }
  14. catch(Cry mail) {
  15. cout << mail.what() << "..\n";
  16. cout << "I\'m very glad to see you!" << endl;
  17. }
  18. catch(...) {
  19. cout << "Bye forever ;(" << endl;
  20. }
  21. return 0;
  22. }
thanks for posting,but can u plz explain a bit?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: How To Return to Main()

 
0
  #6
Mar 10th, 2009
Originally Posted by arshad115 View Post
...but can u plz explain a bit?
Better look at http://www.cprogramming.com/tutorial/exceptions.html or search Google for C++ exception handling.
It's possible to use exceptions for the program control (however as usually it's considered as a bad style).
I think no special problems to design your menu handling to provide fast return to the main loop without gotos and exceptions...
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 149
Reputation: monkey_king is on a distinguished road 
Solved Threads: 8
monkey_king monkey_king is offline Offline
Junior Poster

Re: How To Return to Main()

 
0
  #7
Mar 11th, 2009
goto's can only jump to labels within the same function,
so I don't think you will be able to do it with goto's if you are in some far away function.

There are any beautifull way of doing what you want without having to redesign you program or use c++ exceptions.

On a general note goto should be avoided.
I've only found them usefill in some crazy switch statements.
Which basicly changes it to a jump table.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 53
Reputation: arshad115 is an unknown quantity at this point 
Solved Threads: 2
arshad115's Avatar
arshad115 arshad115 is offline Offline
Junior Poster in Training

Re: How To Return to Main()

 
0
  #8
Mar 11th, 2009
i have functions that call "other functions",and i want to make a menu that return the user from the "other functions" to the main directly,but i also have a while loop in main,and some code above it,i want to goto the while loop from the "other functions"!
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 320
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 63
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: How To Return to Main()

 
0
  #9
Mar 11th, 2009
Reset your var..Cleanup..Then call menu..It's hard to read your codes..esspecialy if it invinsible..
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 1,968
Reputation: tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute tux4life has a reputation beyond repute 
Solved Threads: 214
tux4life's Avatar
tux4life tux4life is offline Offline
Posting Virtuoso

Re: How To Return to Main()

 
0
  #10
Mar 11th, 2009
Maybe you should just try to call 'return <something>' in your code ...
After the return instruction no code is executed anymore so the function will go back to 'main()' (if the function is called from the 'main()' function)

P.S. : Can you please post us some code so we can take a look at what exactly you do mean?
Last edited by tux4life; Mar 11th, 2009 at 2:57 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC