Thread: Menu?

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    2

    Menu?

    How do i create options, almost like a menu?
    I am planning to make a calculator program that works like this:
    1:Person will pick from a menu, 1)Multiply,2)divide, etc., etc.
    2: Program will ask for two numbers, depending on what option person picked, program will complete the function.....

    all i need help with is creating a menu, and how do i set a variable so that it can be a decimal number?

  2. #2
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    A simple menu-based program.

    Code:
    #include <string>
    #include <iostream>
    using namespace std;
    
    bool StringToInt( string s, int& pInt );
    int  menu();
    
    int main(int argc, char *argv[])
    {
      string line;
      int choice = menu();
      cout << "Your choice: " << choice << endl;
      cout << "Press Return to continue..." ;
      getline( cin, line );  
    }
    
    /*
     *  Displays the menu and allows the user to make a choice.
     */
    int menu()
    {
      int choice;
      string line;
      
      while( true )
      {
        cout << "\nChoose your favourite hero!\n\n"
                "  1\tBatman\n\n"
                "  2\tSpiderman\n\n"
                "  3\tThe Incredible Hulk\n\n"
                "  4\tWolverine\n\n"
                "  5\tBritney Spears\n\n"
                "Your choice [a/b/c/d]: ";
        
        getline( cin, line );
        
        if( StringToInt(line, choice) == true )
        {
          if( choice < 0 || choice > 5 )
            cout << "Please only enter numbers between 0 to 5 !\n";
          else
            return choice;
        }      
        else
          cout << "Please only enter integers!\n";
    
        cout << "Press Return to continue..." ;
        getline( cin, line );
      }
    }
    
    /**
     *  Converts a string to an integer.
     *  If the conversion fails, return false.
     */
    bool StringToInt( string s, int& pInt )
    {
      char * end ;
      const char* start = s.c_str();
      pInt = strtol( start, &end, 0 );
     
      if ( start == end )   // end pointer left at start; totally invalid number
        return false;
     
      if ( *end )           // end pointer left in middle; partially invalid number
        return false;
       
      return true;          // end pointer left at end; a valid number
    }
    and how do i set a variable so that it can be a decimal number?
    Use a float or double type.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Posts
    2
    thanks man but how do you use a float or a doubly type?

  4. #4
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    Code:
    float num = 2.0f;

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    62
    Surely if someone is new, a switch statment would be easier to follow and understand?

    Only because a menu using a swtich statment is a lot more simple then that one.

    Chris
    Last edited by chrismax2; 03-20-2005 at 04:27 PM.

  6. #6
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    You still need to read the input and ensure that it's a proper integer.

    And he can use the menu() function in a switch statement.

    Code:
    switch( menu() )
    {
      case 1: /* batman */
      ...
      ...
    }

  7. #7
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    There's a lot of code there for something as simple as a menu, here's the basic outline of what i use, so far it's done the basic tasks i want it to do.

    Code:
    int i;
    cout<< "1. Option one";
    cout<< "2. Option two";
    cout<< "3. Option three";
    cout<< "Choice: "  // The couts will print out your menu
    cin>> i; //reads your choice
    switch (i)
    {
                Case 1: // Executes what you want if number one is pressed
                           blah blah
                           break;
                Case 2: // Executes what you want if number two is pressed
                           blah blah
                           break;
                Case 3: // etc
                           blah blah
                           break;
    }

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    There's a lot of code there for something as simple as a menu, here's the basic outline of what i use, so far it's done the basic tasks i want it to do.
    Quote Originally Posted by Dante
    You still need to read the input and ensure that it's a proper integer.
    If a non-integer is entered, your menu will most likely go into an infinite loop and flash by a million times a minute until you kill the process. Although, as long as you never enter 'bad' choices, that shouldn't be a problem.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  9. #9
    Let's do some coding! Welshy's Avatar
    Join Date
    Mar 2005
    Location
    Staffordshire University, UK
    Posts
    168
    Hunter2: you have a point there mate, i must have missed that part, but it shouldnt be a problem as long as only integers are entered......cant say im too good with input validation at the minute though so i cant give any advice on that area

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Replies: 2
    Last Post: 03-05-2009, 10:25 AM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Constructive Feed Back (Java Program)
    By xddxogm3 in forum Tech Board
    Replies: 12
    Last Post: 10-10-2004, 03:41 AM