Thread: getline won't work

  1. #1
    Registered User
    Join Date
    Nov 2013
    Posts
    18

    getline won't work

    Hello everyone.

    I've just started learning from "Jumping into C++". Great book. Unfortunately, I've also encountered my first snag. Practice problem number 3 from chapter 4 tells me to make a small calculator that takes one of the four arithmetic operations and its two arguments as input and give the result as output. Here's what my newbie mind came up with:

    Code:
    #include <iostream>
    #include <string>
    
    
    using namespace std;
    
    
    int main()
    {
        int a;
        int b;
        string operation;
        cout<<"a= "<<endl;
        cin>>a;
        cout<<"b= "<<endl;
        cin>>b;
        cout<<"What operation would you like performed? (add, subtract, divide, multiply)"<<endl;
        getline(cin, operation, '\n');
        if(operation=="add")
        {
            cout<<"a+b="<<a+b;
        }
        else if(operation=="subtract")
        {
            cout<<"a-b="<<a-b;
        }
        else if(operation=="divide")
        {
            cout<<"a/b="<<a/b;
        }
        else if(operation=="multiply")
        {
            cout<<"a*b="<<a*b;
        }
        cin.ignore();
        cin.get();
    }
    The thing that I don't get is why doesn't the function "getline" work. It's worked in previous programs. The program seems to work if I simply replace getline with a simple "cin". I could easily use that as a cheap fix but I am interested in knowing why "getline" refuses to work anymore.

    I hope someone can enlighten me on this.

    Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This leaves the newline entered in the input buffer:
    Code:
    cin>>b;
    This newline is then consumed by the getline call. Three ways to fix this comes to mind:
    • Stick to using the formatted input with operator>>, if feasible.
    • Use a cin.ignore call somewhere between the cin>>b and getline.
    • Stick to using getline, then parse the input with a stringstream when you need formatted input.

    I tend to prefer the third solution, though one of the first two would probably be easier for you at the moment.
    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
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, let's look at what the user types in. Suppose the user inputs 4, 7, and multiply. The input stream would then hold "4\n7\nmultiply" (where \n = "Enter" or "Return"). cin >> a grabs the 4. cin >> b skips the \n (because that's not a number) and then grabs the 7. getline then grabs up to the next \n............

    (Generally, mixing >> and getline is a fool's game.)

  4. #4
    Registered User
    Join Date
    Nov 2013
    Posts
    18
    Thanks for the help guys. Now that I understand the problem I think I'll stick with cin until I find a truly useful advantage of using getline.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Annoying getline does not work
    By kulfon in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2011, 05:16 AM
  2. Why first getline doesnt work?
    By kulfon in forum C++ Programming
    Replies: 3
    Last Post: 02-21-2011, 10:17 AM
  3. Getline not wanting to work, for me
    By jerimo in forum C++ Programming
    Replies: 6
    Last Post: 07-17-2010, 09:46 AM
  4. why wont getline() work?
    By lilhawk2892 in forum C++ Programming
    Replies: 1
    Last Post: 03-30-2009, 09:55 PM
  5. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM