Thread: How to restart a program without closing it...

  1. #16
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    There is no excuse for a goto statement. Goto statements are the way you program if you do ASM, or some crummy thing like PBasic (which I had to learn as it would allow programming of one type of microcontroller). Goto statements are what you use when there IS nothing better.

    It is actually much HARDER to program with gotos that without. No good program will ever use them. It is never the best way, and never the most intuitive way.

    A menu works MUCH better like this:

    Code:
    char choice;
    bool inMenu = true;
    while(inMenu){
    	PresentPrompt();
    	cin >> choice;
    	switch(choice){
    		case 'a':
    		case 'A':
    			KeyA();
    			break;
    		// Other cases as needed
    		case 'q':
    		case 'Q':
    			DoCleanup();
    			inMenu = false;
    			break;
    	}
    }
    Using this, you can see at a glance how flow happens. With gotos, your code becomes much more convuluted, your flow becomes tricky to decipher, and there are many kinds of errors you can accidently cause.

  2. #17
    Registered User
    Join Date
    Sep 2001
    Posts
    412
    And, BTW, the best way to do a nested menu (go from main meu to another menu, etc.) is just to call a function which does the submenu, and will not return until the user exits the submenu. Then, each menu function is one single loop, with one single switch statement. Moving to a child menu is just a call to a function, moving back to a parent is just a return. You can even do the main menu like that, so your program is just something like:

    Code:
    int main(int, char**){
    	//initialize stuff if needed
    	MainMenu();
    	//do cleanup
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. exiting and closing a program
    By major_small in forum C++ Programming
    Replies: 10
    Last Post: 05-30-2003, 08:31 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM