Thread: Break from loop to main();

  1. #1
    Registered User
    Join Date
    Mar 2005
    Location
    Freeport, IL
    Posts
    32

    Break from loop to main();

    I have a little program I'm writing and if I have a while loop in a function and I use the break; code to cancel the loop if a condition is met other than the normal while condition is there a way to send the program back to the beginning of like main() or to another function?


    Thanks

    Douglas

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you can use another loop

    Code:
    for(;;) /* this is the beginning of main */
    {
       /* here goes some stuff */
       while(condition)
       {
           if(condition2)
             break; /* so you break the while loop and go to the beginning of the for loop
          /* more stuff */
       }
       /* break bring you here - and you can break the for loop or go to the begining of it */
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Freeport, IL
    Posts
    32
    so I'm assuming that then if you want out of the program all together you just put a condition to possibly select a exit();

    Thanks for the reply.

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    select a exit();
    better just
    return errcode;
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  5. #5
    Registered User
    Join Date
    Mar 2005
    Location
    Freeport, IL
    Posts
    32
    I found another solution for my original question that works very well also and could be a help to other newbie programmers like myself. A nice do while loop in the int main() area.

    Works great.

    Code:
    int main() {
    
    	int menu;
    	do {
    		cout << "1. Registrar info" << endl;
    		cout << "2. Contact info" << endl;
    		cout << "3. Exit Program" << endl;
    		cout << "Enter a menu number: ";
    		cin >> menu;
    	} while (menu != 3);
    	return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. I need help with variables, please
    By JOCAAN in forum C++ Programming
    Replies: 39
    Last Post: 12-08-2007, 04:16 PM
  3. Display Lists (OpenGL)
    By Shamino in forum Game Programming
    Replies: 11
    Last Post: 06-05-2005, 12:11 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. error with code
    By duffy in forum C Programming
    Replies: 8
    Last Post: 10-22-2002, 09:45 PM