Thread: Help with C++

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

    Help with C++

    I'm attempting practice problem #2 in Ch.5 from "Jumping Into C++" by Alex Allain. Can someone please shed some light on why my code isn't working properly?
    Code:
    #include <iostream>#include <string>
    
    
    using namespace std;
    
    
    int main ()
    {
        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: ";
                cin >> entree_selected;
            } 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;
            }
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Recall that you would only read a "word" with cin >> entree_selected. Try std::getline instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2014
    Posts
    6
    You might also consider assigning each options a letter or number and taking a charor int as an input.

    Choose your dinner selection:
    1: Prime Rib
    2: Lasagna
    3: Chicken Dinner

    I would also make the cout statements that are a consequence of your 'if/else if' statements individual for testing purposes.

  4. #4
    Registered User
    Join Date
    Nov 2015
    Posts
    8
    Thanks so much for your help, guys. Problem solved!

Popular pages Recent additions subscribe to a feed