-1

好吧,我正在准备考试,所以我试图让我的代码尽可能简单,但后来发生了一些非常奇怪的事情:练习是更改以下代码,并通过使用异常返回主菜单。

这是问题的代码:

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 

我所做的是:

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();
    }
}

一开始它工作得很好,但有一次它用未处理的异常消息终止了我的程序。为什么?

最简单的正确方法是什么?

编辑:这是一种工作方式:

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();
}
4

2 回答 2

5

我只会回答“怎么来”的部分,因为最简单的正确方法是您应该自己解决的问题。当你这样做

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

你执行mainloop,捕捉任何异常。当捕获到异常时,处理程序会在block之外mainloop再次执行。你会想要反复进入,每次都捕捉到异常。trymainloop

于 2012-07-09T22:18:34.723 回答
0

好的,这是我的建议。

首先,ask_return()需要写出来。这很简单,据我所知,你或多或少是正确的。我将建议实际使用std::exception虽然:

void ask_return()
{
    char c;
    cout << "Return to main menu? y/n" << endl;
    cin >> c;
    if ( c == 'y' )
    {
        throw std::exception(); // might as well use the class designed for this
    }
}

有很多地方可以捕捉到异常,但最简单的地方是mainloop()

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;

        try
        {
            switch (answer[0])
            {
                case '1':
                    ha_ha_loop();
                case '2':
                    dollar_loop();
                case '3':
                    return;
            } // switch 
        }
        catch (std::exception& e)
        {
            // do nothing, let the loop re-run.
        }

    } // while 

} // mainloop 

在这种情况下,您甚至不需要对异常做任何事情:您只需让函数继续循环,它就会工作。

于 2012-07-09T22:27:02.573 回答