Personally, I sometimes use while(1) or similar for things like menus, where choosing a menu option would cause the program to do something and then return to the menu. Then, if you want to exit the program, just return 0 to end the program:
int main()
{
int bob;
while(1)
{
cout << "Enter a number to display, or -1 to exit: ";
cin >> bob;
if(bob == -1) { return 0; }
cout << bob << endl;
}
}
This is generally not recommended, although it does work. The reason is you are burying the exit from the function/program in the middle of your code. This could be a maintenance nightmare for the team that inherits your program (or for you in 2 months

) It would be best to break out of the loop and return at the bottom of the function.
Reputation Points: 3281
Solved Threads: 895
Posting Sage
Offline 7,747 posts
since May 2006