Well, I'm studying for an exam, so I tried to keep my code as simple as possible, but then something strange Happened: Exercise had to change the following code and use the exceptions to return to the main menu.
Here is the question code:
void ha_ha_loop () {int i, c; While (1) {for (i = 0; i & lt; 3; i ++) {cout & lt; & Lt; "Ha ha ha" & lt; & Lt; Endl; Sleep (3); } // for ask_return (); } // while} // ha_ha_loop zero dollar_loop () {int i; While (1) {for (i = 0; i & lt; 3; i ++) {cout & lt; & Lt; "$$$$$$$$$" & lt; & Lt; Endl; Sleep (3); } // for ask_return (); } / * While * /} // dollar_loop void mainloop () {string reply; While (1) {cout & lt; & Lt; "Ha ha ha 1 press." & Lt; & Lt; Endl; Cout & lt; & Lt; Press 2 for "$$$$$$$$." & Lt; & Lt; Endl; Cout & lt; & Lt; "Press 3 to leave." & Lt; & Lt; Endl; Cin & gt; & Gt; To answer; Switch (Answer [0]) {Case '1': ha_ha_loop (); Case '2': dollar_loop (); Case '3': Return; } // switch} // while} // mainloop and what I did:
zero ask_return () {char c; Cout & lt; & Lt; "Go back to the main menu? Y / n:" & lt; & Lt; Endl; Cin & gt; & Gt; C; If throw (C == 'Y') 1; } Zero ha_ha_loop () {int i, c; While (1) {for (i = 0; i & lt; 3; i ++) {cout & lt; & Lt; "Ha ha ha" & lt; & Lt; Endl; } // for ask_return (); } // while} // ha_ha_loop zero dollar_loop () {int i; While (1) {for (i = 0; i & lt; 3; i ++) {cout & lt; & Lt; "$$$$$$$$$" & lt; & Lt; Endl; } // for ask_return (); } / * While * /} // dollar_loop void mainloop () {four answers; While (1) {cout & lt; & Lt; "Ha ha ha 1 press." & Lt; & Lt; Endl; Cout & lt; & Lt; Press 2 for "$$$$$$$$." & Lt; & Lt; Endl; Cout & lt; & Lt; "Press 3 to leave." & Lt; & Lt; Endl; Cin & gt; & Gt; To answer; Switch (Answer) {Case '1': ha_ha_loop (); Case '2': dollar_loop (); Case '3': Return; }}} Int main () {try {mainloop ()}} hold (...) {mainloop (); }} It works fine in the beginning, but once again it ends up with an unrestricted exception message to my program. Why?
What is the easiest way to do this?
Edit: This is a working way:
zero ask_return () {char C; Cout & lt; & Lt; "Go back to the main menu? Y / n:" & lt; & Lt; Endl; Cin & gt; & Gt; C; If throw (C == 'Y') 1; } Zero ha_ha_loop () {int i, c; While (1) {for (i = 0; i & lt; 3; i ++) {cout & lt; & Lt; "Ha ha ha" & lt; & Lt; Endl; } // for ask_return (); } // while} // ha_ha_loop zero dollar_loop () {int i; While (1) {for (i = 0; i & lt; 3; i ++) {cout & lt; & Lt; "$$$$$$$$$" & lt; & Lt; Endl; } // for ask_return (); } / * While * /} // dollar_loop void mainloop () {four answers; While (1) {try {cout & lt; & Lt; "Ha ha ha 1 press." & Lt; & Lt; Endl; Cout & lt; & Lt; Press 2 for "$$$$$$$$." & Lt; & Lt; Endl; Cout & lt; & Lt; "Press 3 to leave." & Lt; & Lt; Endl; Cin & gt; & Gt; To answer; Switch (Answer) {Case '1': ha_ha_loop (); Case '2': dollar_loop (); Case '3': Return; }} Hold (...) {}}} int main () {mainloop (); }
I will only respond to "how" part, because the easiest way is right You should solve yourself when you do
try {mainloop ()}} hold (...) {mainloop (); } To execute any exception, execute mainlop . When an exception is caught, the handler runs mainlop again, a try block . You want to go to mainloop every time to catch exceptions.
Comments
Post a Comment