Break from loop to main();

This is a discussion on Break from loop to main(); within the C++ Programming forums, part of the General Programming Boards category; I have a little program I'm writing and if I have a while loop in a function and I use ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    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 */
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,471
    select a exit();
    better just
    return errcode;
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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, 03: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21