Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8221203
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:49:29+00:00 2026-06-07T13:49:29+00:00

Well, I’m studying for an exam so I tried to keep my code as

  • 0

Well, I’m studying for an exam so I tried to keep my code as simple as possible, but then something very weird happened: the exercise was to change the following code and, by using exceptions, get back to the main menu.

Here’s the code of the question:

void ha_ha_loop()
{
 int i, c;

 while(1)
  {
     for(i=0; i < 3; i++)
     {
       cout << "Ha Ha Ha" << endl;
       sleep(3);
     } // for 
    ask_return(); 
  } // while 

} // ha_ha_loop 

void dollar_loop()
{

 int i;

   while(1)
    {
     for(i=0; i < 3; i++)
      {
        cout << "$$$$$$$$$ " << endl;
        sleep(3);
      } // for 

      ask_return(); 
    } /* while */

} // dollar_loop 

void mainloop()
{
  string answer;

 while (1)
 {

    cout << "Press 1 for Ha Ha Ha." << endl;
    cout <<  "Press 2 for $$$$$$$$." << endl;
    cout << "Press 3 for to quit." << endl;

    cin >> answer;

    switch (answer[0])
    {
      case '1':
       ha_ha_loop();
      case '2':
       dollar_loop();
      case '3':
       return;
    } // switch 

  } // while 

} // mainloop 

And what I did was:

void ask_return() {
    char c;
    cout << "Return to main menu? y/n:"<<endl;
    cin >> c;
    if (c=='y') throw 1;
}

void ha_ha_loop()
{
 int i, c;

 while(1)
  {
     for(i=0; i < 3; i++)
     {
       cout << "Ha Ha Ha" << endl;

     } // for 
    ask_return(); 
  } // while 

} // ha_ha_loop 

void dollar_loop()
{

 int i;

   while(1)
    {
     for(i=0; i < 3; i++)
      {
        cout << "$$$$$$$$$ " << endl;
      } // for 

      ask_return(); 
    } /* while */

} // dollar_loop 

void mainloop()
{
  char answer;

 while (1)
 {

    cout << "Press 1 for Ha Ha Ha." << endl;
    cout <<  "Press 2 for $$$$$$$$." << endl;
    cout << "Press 3 for to quit." << endl;

    cin >> answer;

    switch (answer)
    {
      case '1':
       ha_ha_loop();
      case '2':
       dollar_loop();
      case '3':
       return;
    }
  }

}



int main() {
    try {
        mainloop();
    } catch (...) {
        mainloop();
    }
}

It works fine at the beginning, but then after one time it terminates my program with an unhandled exception message. Why?

What is the simplest correct way to do that?

Edit:
this is a working way :

void ask_return() {
    char c;
    cout << "Return to main menu? y/n:"<<endl;
    cin >> c;
    if (c=='y') throw 1;
}

void ha_ha_loop()
{
 int i, c;

 while(1)
  {
     for(i=0; i < 3; i++)
     {
       cout << "Ha Ha Ha" << endl;

     } // for 
    ask_return(); 
  } // while 

} // ha_ha_loop 

void dollar_loop()
{

 int i;

   while(1)
    {
     for(i=0; i < 3; i++)
      {
        cout << "$$$$$$$$$ " << endl;
      } // for 

      ask_return(); 
    } /* while */

} // dollar_loop 

void mainloop()
{
  char answer;

 while (1)
 {
    try {
    cout << "Press 1 for Ha Ha Ha." << endl;
    cout <<  "Press 2 for $$$$$$$$." << endl;
    cout << "Press 3 for to quit." << endl;

    cin >> answer;

    switch (answer)
    {
      case '1':
       ha_ha_loop();
      case '2':
       dollar_loop();
      case '3':
       return;
    }
    } catch (...) {
    }
  }

}



int main() {
    mainloop();
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T13:49:33+00:00Added an answer on June 7, 2026 at 1:49 pm

    I’ll only answer the “how come” part, since the simplest correct way is what you’re supposed to solve yourself. When you do

    try {
        mainloop();
    } catch (...) {
        mainloop();
    }
    

    you execute mainloop, catching any exceptions. When an exception is caught, the handler executes mainloop again, outside of a try block. You’ll want to repeatedly go into mainloop, catching the exception every time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Well, it may actually be a simple case but I'm having a tough time
Well friends, I have got this query which works but is very long for
Well... simple question, right? But with no so simple answers. In firefox i use
Well, it seems simple enough, but I can't find a way to add a
Well I used css3pie in asp.net which is not working. I tried every possible
Well, my website can not redirect to https://www.facebook.com/QuaFootSpa from http://quafootspa.com/ I have tried redirection
Well, I have a application which somehow requires some system resources, but how do
Well, I was trying to fix this program, and I keep getting the errors
Well im here because i have a problem. i have code that was created
Well I created a jquery mobile form but the every element is in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.