Thread: help with getline() please.

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    163

    help with getline() please.

    Well, I'm stumped again. This is a pretty simple problem. Get info from user for 3 products, sort them by price and display in a table. So, I have included the string library, and I'm using getline(cin, userString, '\n') to get the description from the user. It works fine the first time through, but on second and third times it does the cout right before the getline prompt then skips to the next cout(for serial number). It's not wrong syntaxually (sp?), I'm just wondering if there's something I'm missing or if it's my compiler. Here's the code
    Code:
    int main()
    {
    
        string userString1, userString2, userString3;
        int serial1, serial2, serial3;
        double price1 = 0, price2 = 0, price3 = 0;
        char yesNo = 'n';
        
        cout << "Please enter information:" << endl;
        cout << "Brief Description: ";
        getline(cin, userString1, '\n');  //works here
    
        cout << "Serial number: ";
        cin >> serial1;
        cout << "Price: $";
        cin >> price1;
    
    
        cout << "Please enter information:" << endl;
        cout << "Brief Description: ";
        getline(cin, userString2, '\n'); //doesn't work here
           
        cout << "Serial number: ";
        cin >> serial2;
        cout << "Price: $";
        cin >> price2;
        
        
        cout << "Please enter information:" << endl;
        cout << "Brief Description: ";
        getline(cin, userString3, '\n');
        cout << "Serial number: ";
        cin >> serial3;
        cout << "Price: $";
        cin >> price3;
    
        cout << "Thank you." << endl;
        cout << "Ready to display table(Y or N)? ";
        cin >> yesNo;
        Sorter(price1, serial1, userString1, 
               price2, serial2, userString2,
               price3, serial3, userString3);
        cin >> yesNo;
        return(0);
    }
    can anybody help? thanks!

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You need to flush the input buffer before each getline(). It's come up at least five times in the past week -- try a board search or read the FAQ.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> You need to flush the input buffer before each getline().
    In general that is not correct. You need to do it after each call to cin >> that precedes a call to getline. Or if you prefer, after each call to cin >>.

    The easiest way to do this is to call cin.ignore() after each cin>> call. This ignores the trailing newline (or other whitespace) from when the user enters the response.

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    2

    another question in this matter

    How can I ignore extra spaces in the input?
    I am not alowd to use flush, fflush or anything like that.

    I have to get a line (optionaly with too many spaces) and than separate to words (with no spaces). I used getline(cin, str).
    .

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Use getline() for ALL input, then convert the result from the in-memory string.

    Use a string stream if you still want to do >> myIntVar; type of thing.
    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.

  6. #6
    Registered User
    Join Date
    Aug 2006
    Posts
    2
    Well, I tried this code. It works untill the final loop, than it crush.
    Code:
         getline(cin, str, '\n'); //I also tried without '\n'
    
         //now replacing every 2 ' ' with one ' '
         while (str.find("  ")) {//ok untill last loop - than flying...
               str.replace(str.find("  "), 2, " ");
         }
    Than I tried this code, but it didn't compile...
    Code:
         getline(cin, str, '\n'); //I also tried without '\n'
    
         //another stop condition for the loop
         while (str.find("  ") != str.end()) {//my compiler almost killed me...
               str.replace(str.find("  "), 2, " ");
         }
    If anyone can tell my what's the problem I'd be thankful.
    Thank you
    .

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >while (str.find(" ") != str.end()) {//my compiler almost killed me...
    std::string's find member function returns std::npos on failure, or a valid index into the string represented by std::string::size_type, not an iterator.
    My best code is written with the delete key.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    std::string::npos, not std::npos. At least I have never heard of a std::npos.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >std::string::npos, not std::npos.
    Yes, I was typing too fast for my brain to keep up.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. getline problem
    By Bitphire in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2004, 04:42 PM
  3. getline help
    By ProjectsProject in forum C++ Programming
    Replies: 3
    Last Post: 06-14-2004, 11:12 AM
  4. getline not working right
    By talz13 in forum C++ Programming
    Replies: 11
    Last Post: 12-10-2003, 11:46 PM
  5. getline with string class in Borland C++
    By johnnyd in forum C++ Programming
    Replies: 6
    Last Post: 03-08-2003, 02:59 PM