Menu

Pages: 12
Hey I'm new at coding and I have a project where I need to create two games and have the user pick which game they would like to play. I already have the codes for the two games i just need help on how to put them together and have the user be able to pick which game they would like to play.
Last edited on
Do you know how to use cin?

Do you know how to code an if statement?

Do you know how to initiate either of the two games?
yeah i know how to use cin and code im just beginning but i have both of the games in different c++ windows
Last edited on
OK. Is this a project that you were assigned by an instructor, a self-defined project for a class, or a fun project you are doing on your own to learn C++? Your answer to this question will help others understand what you are trying to accomplish. (Your initial description was a bit vague, so I'm trying to get some more details so that we can try to help you.)
its a project from my instructor i think a switch menu would be best but i just need help starting out
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// Pick a game menu

#include<iostream>
using namespace std;
int main ()

{//open of main
	int select;
	
	cout << " Please type in one number : \n\n\t\t1 for GAME A \t\t2 for GAME B"<< endl;
	cin >> select;
	
	switch (select)
	{//open of switch
		case 1: cout << "You have selected GAME A " ; break;
		case 2: cout << " You have selected GAME B " ; break;
		default:
			cout << "You entered a wrong number" << endl; 
		 
	} // end of switch
	return 0;
} //end of main 
the two codes are in different windows so i just need to create a menu like for option 1: game 1 or option 2: game two and then combine the codes
where would i put my coding in that for if they chose game a or b?
would i after the default: part put:

cin >> choice;

switch (choice)
{
if game 1 chosen:
//code for game a
break;
else game 2 chose:
code for game b
break;
You could make the "Games" objects and call them. Then give them both a "Play" function (what the main is now probably) and call that for game a or game b in the menu. You could alternatively combine them and put each main in a function called like gameA() and gameB() then call accordingly.
Last edited on
By "windows" do you mean do separate programs, two separate executable files? That probably isn't what your professor wants, that would require a bunch more stuff like platform-dependent code to call an executable from another executable.

Your question is vague. If the user chooses game 1, then you'd run the code that starts game one.
1
2
3
4
5
6
7
8
        case 1:
            cout << "You have selected GAME A " ;
            startGame_A(); // or how ever you start your game
            break;
        case 2: 
           cout << " You have selected GAME B ";
             startGame_B();
            break;
Last edited on
yeah they are in different files i figured i would be able to copy and paste them in once i figured out the menu
would i after the default: part put:

cin >> choice;

switch (choice)
{
if game 1 chosen:
//code for game a
break;
else game 2 chose:
code for game b
break;
Just saw your post now, that would be a very large main function. Generally you want to break things down into sub functions. Anything that is used more than once you definitely want to put in sub functions.
Last edited on
my question is basically how do i make a switch menu and where would i put my coding in that are in two different files that i would copy and paste
Last edited on
so can a program have many different main functions like can my menu have a main function and my two games have different main functions?
Not within the same program, only one int main() per program.
so then either way it's going to be a large main function

Let's say your first program source file for Game 1 looks like this.
1
2
3
4
5
6
int main()
{
   //a bunch of lines of code for your game:
  //code
  //more code
}


Instead of doing everything in main, copy the stuff into a playGame1() function.

1
2
3
4
5
6
7
8
9
10
11
void playGame1()
{
   //a bunch of lines of code for your game:
  //code
  //more code
}

int main()
{
    playGame1();
}


See where I'm going? Do the same for game2. Now, each game is a separate function that runs it. Now when you're combining source, you don't copy everything into main(), main() should just have a way to call your other game functions.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void playGame1()
{
   //put game 1's code here (not including the actual definition of main!)
}
void playgame2()
{
  //put game 2's code here
}

int main()
{
   //pick whether to:
   playGame1();
   //or
   playGame2();
}
Last edited on
i kinda get it thanks, but im still kinda confused on where to put everything. So i put the menu coding and then put void playGame1 stuff?
so the int main() is acting like the menu then?
Pages: 12