How to I loop back to my main menu with my switch statements?

Bear with me, I'm still pretty new to C++...

So my dilemma is that I have a menu for 2 games, both within their respective switch statement/cases. The code works, but it'll close the game once it's done. I can't for the life of me figure out how to loop back to the main menu that I made to select the games.

My code is the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include<iostream>
#include<string>
#include<cstdlib>
#include<ctime>
#include<iomanip>

using namespace std;

int main()
{
  string name;
  int choice; //the choices for the menu
  char sym; //part of the menu
  int number = rand() % 10 + 1; //sets number to a random number between 1-10
  int digit; // part of the math game
  bool menu = true;
  int guessNum; //number that user guesses
  int i; //loop variable
  bool guessRight = false;
  char answer = 'y';



  //questions omitted
  while (menu){ //start of loop and will come back here after each game finishes
    cout << setfill(sym) << setw(55);
    cout << "\n";
    cout << "\t Welcome," << ' ' << name << endl;
    cout << "Please choose a number from the following options:" << endl; //Gives the user the options to input using numbers.
    cout << "\n";
    cout << "\t1. Play the math game!" << endl;
    cout << "\t2. Play the guessing game!" << endl;
    cout << "\t3. Exit" << endl;
    cout << setfill(sym) << setw(55);
    cout << "\n";
    cin >> choice; //The user gets to input numbers 1-3 at this point

    switch (choice)
    {
      case 1:
        //Math game here
        break;

      case 2:
        //random number game here
        break;

      case 3: //exits the program
        cout << "I guess we're done here." << endl;
        return 0;
    }
  }
}


I've omitted the games themselves, but I'm confused as to how to loop back to the main menu from both games. I suspect that I have to inset a do-while loop but I'm not sure how to go about doing that. I appreciate any advice!
You could make them functions. Main menu as a function, then each game as a function. At the end of a game, call the main menu function.
Hi,

Search this site for bool controlled while loop you should find them with a switch inside.

There is a search bar at the top of this page.

:+)
Since you're still new, you could try cascaded/nested loops of for or while statements.

http://www.tutorialspoint.com/cplusplus/cpp_nested_loops.htm
Last edited on
Topic archived. No new replies allowed.