Thread: Simple Menu Program

  1. #1
    Registered User
    Join Date
    Mar 2014
    Location
    London, UK
    Posts
    3

    Simple Menu Program

    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: ";
    
    
    }

  2. #2
    Registered User
    Join Date
    Mar 2014
    Location
    London, UK
    Posts
    3
    hmm, while I said it works I have noticed a couple of bugs .. if you select character bios then select player 1, then go back to the bios menu, then select player 1 again it doesn't show player 1's bios, instead it returns to the main menu. I feel my code isn't very efficient.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 08-28-2010, 12:58 AM
  2. A simple Menu Problem
    By nebula1 in forum C Programming
    Replies: 1
    Last Post: 10-14-2009, 05:33 PM
  3. Create a very simple menu
    By ulillillia in forum Windows Programming
    Replies: 8
    Last Post: 03-22-2009, 08:18 PM
  4. Simple Menu Questions
    By Olidivera in forum Windows Programming
    Replies: 4
    Last Post: 06-03-2006, 05:29 PM
  5. Help!!! Simple Menu Program
    By ghofigjong in forum C Programming
    Replies: 6
    Last Post: 12-02-2002, 04:19 PM

Tags for this Thread