Hi there I'm fairly new to programming, I decided to create a simple program to display a menu with options, while that is easy enough I had some difficulty when selecting an option that has options inside which also has an option again (sorry for the confusion). While I had many issues in the past I'm finally finished with it and it works fine, but being new to programming and not knowing various other methods available, would you say this is the most efficient way of creating what I needed? or is there a far more optimized way??
Code:
//:::::::::::::::::::::Simple Menu Program::::::::::::::::
//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#include <iostream>
#include <string>
//function prototypes
//========================================
void Menu();
void Story();
void Bios();
void Player1();
//========================================
//global variables
//========================================
int choice(0); //selection variable
//========================================
int main()
{
do//while in the Menu() function display it untill the user selects option 3
{
system("CLS");//clear screen of any previous or future menus
Menu();//display the main menu
std::cin >> choice;//user inputs choice
switch(choice)//determine what happens based on choice
{
case 1://if option 1 is selected
system("CLS");//clear the screen
Story();//display the games story
std::cin >> choice;//get choice from player
if(choice == 1)//when in Story() if option 1 is selected
{
system("CLS");//clear screen
Menu();//return to the Main Menu
}
else if(choice == 2)//when in Story() if option 2 is selected then
{
exit(0);//exit program
}
break;
case 2://if option 2 is selected in the Main Menu
system("CLS");//clear the screen
Bios();//display the Bios() menu
std::cin >> choice;//user inputs choice
if(choice == 1)//when in Bios() if option 1 is selected
{
system("CLS");//clear screen
Player1();//display p1's bios
//---------------------------------------------
//in p1's bios you have the option to press 1 to go back to Bios() or 2 to exit
//---------------------------------------------
std::cin >> choice;//get input of 1 or 2 from user
if(choice == 1)//if option 1 is chosen
{
system("CLS");//clear screen
Bios();//display Bios() again
std::cin >> choice;
}
else if(choice == 2)//while viewing p1's bios if selecting option 2 then exit
{
exit(0);
}
}
else if(choice == 2)//when in Bios() if option 2 is selected
{
system("CLS");//clear screen
Menu();//display Menu()
}
break;
case 3://if option 3 is selected
exit(0);//terminate the program
break;//necessary??
}
}while(choice!=3);
return 0;
}
void Menu()//Main Menu layout
{
std::cout << "::::::::::Main Menu::::::::::\n" << std::endl;
std::cout << "1) Game Story" << std::endl;
std::cout << "2) Character Bios" << std::endl;
std::cout << "3) Exit Program\n" << std::endl;
std::cout << "Please select an option: ";
}
void Story()//Game Story
{
std::cout << "::::::::::Game Story::::::::::\n" << std::endl;
std::cout << "This is the story of the game...\n" << std::endl;
std::cout << "press 1 to return to the main menu or 2 to exit the program: ";
}
void Bios()//Bios Menu layout
{
std::cout << "::::::::::Bios Menu::::::::::\n" << std::endl;
std::cout << "1) Player 1" << std::endl;
std::cout << "2) Back" << std::endl;
std::cout << "Please select an option: ";
}
void Player1()//Player 1 Bios - I will use a class and objects later for this info
{
std::cout << "::::::::::PLAYER 1::::::::::\n" << std::endl;
std::cout << "Name: Player 1" << std::endl;
std::cout << "Age: 300" << std::endl;
std::cout << "From: Unknown" << std::endl;
std::cout << "Height: 7.0ft" << std::endl;
std::cout << "Weight: 430lbs\n" << std::endl;
std::cout << "press 1 to return to the main menu or 2 to exit the program: ";
}