Thread: Functions...help please

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    8

    Functions...help please

    I am attempting to take a program that I have previously written and break it out into a series of calls to functions. However, when the "menu" program is selected, it prints twice before any data is entered. Been scratching my head for the past few days on this one. Someone please tell me what I'm doing wrong.

    Code:
    #include <iostream>#include <string>
    
    
    using namespace std;
    
    
    int menu()
    {
        string entree_selected;
    
    
        do
        {
            cout << "\nPlease select one of the following options as your meal for this evening:" << endl;
            cout << "- Prime Rib" << endl;
            cout << "- Lasagna" << endl;
            cout << "- Smoked Salmon" << endl;
            cout << "Enter your option: ";
            getline ( cin, entree_selected, '\n');
        } while ( entree_selected != "Prime Rib" && entree_selected != "Lasagna" && entree_selected != "Smoked Salmon" );
    
    
        if ( entree_selected == "Prime Rib" )
        {
            cout << "\nThank you. Enjoy your meal." << endl;
        }
        else if ( entree_selected == "Lasagna" )
        {
            cout << "\nThank you. Enjoy your meal." << endl;
        }
        else if ( entree_selected == "Smoked Salmon" )
        {
            cout << "\nThank you. Enjoy your meal." << endl;
        }
    
    
    }
    
    
    int calculator( )
    {
        float first_argument;
        float second_argument;
    
    
        cout << "\nPlease enter a number: ";
        cin >> first_argument;
        cout << "\nPlease enter another number: ";
        cin >> second_argument;
        cout << "\n" << first_argument << " + " << second_argument << " = " << first_argument + second_argument << endl;
        cout << "\n" << first_argument << " - " << second_argument << " = " << first_argument - second_argument << endl;
        cout << "\n" << first_argument << " * " << second_argument << " = " << first_argument * second_argument << endl;
        cout << "\n" << first_argument << " / " << second_argument << " = " << first_argument / second_argument << endl;
    }
    
    
    void song()
    {
        int i = 99; // Bottle count
    
    
        while ( i > 1 )
        {
            cout << i << " bottles of beer on the wall." << endl;
            cout << i << " bottles of beer." << endl;
    
    
            i--;
    
    
            cout << "Take one down. Pass it around." << endl;
            cout << i << " bottles of beer on the wall.\n" << endl;
    
    
            if ( i == 2 )
            {
                cout << "2 bottles of beer on the wall." << endl
                     << "2 bottles of beer." << endl
                     << "Take one down. Pass it around." << endl
                     << "1 bottle of beer on the wall.\n" << endl;
                cout << "1 bottle of beer on the wall." << endl
                     << "1 bottle of beer." << endl
                     << "Take one down. Pass it around." << endl
                     << "No more bottles of beer on the wall.\n" << endl;
                cout << "No more bottles of beer on the wall." << endl
                     << "No more bottles of beer." << endl
                     << "Go to the store. Buy some more." << endl
                     << "99 bottles of beer on the wall.\n" << endl;
    
    
                     break;
    
    
            }
        }
    
    
    }
    
    
    int main ()
    {
        string program_selected;
    
    
        cout << "Please select one of the following programs. 1) menu, 2) calculator or 3) song: ";
        cin >> program_selected;
    
    
        if ( program_selected == "menu" )
        {
            menu();
    
    
        }
        else if ( program_selected == "calculator" )
        {
            calculator();
    
    
        }
        else if ( program_selected == "song" )
        {
            song();
    
    
        }
        else
        {
            cout << "\nInvalid entry." << endl;
        }
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > cin >> program_selected;
    This reads input, but leaves the \n on the input stream.

    > getline ( cin, entree_selected, '\n');
    This reads input, but uses \n as a delimiter. If you left a \n on the input stream, it immediately returns with an empty string.

    Lookup cin.flush()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    8
    You fixed my code. Thanks for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-27-2013, 06:43 PM
  2. WinAPI functions - similar functions as part of VS C++
    By jlewand in forum Windows Programming
    Replies: 2
    Last Post: 02-02-2012, 08:54 AM
  3. Creating Functions & passing information to other functions
    By RyanLeonard in forum C Programming
    Replies: 4
    Last Post: 10-28-2010, 12:17 PM
  4. Replies: 6
    Last Post: 05-06-2003, 03:08 PM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM

Tags for this Thread