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.
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.)
// Pick a game menu
#include<iostream>
usingnamespace 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
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.
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;
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.
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();
}