std::getline and cin

This is a discussion on std::getline and cin within the C++ Programming forums, part of the General Programming Boards category; hi, I have this code: PHP Code: ... string BookName ,  Author ; cout  <<  "> Enter book title: " ; getline ( cin ,  ...

  1. #1
    Registered User
    Join Date
    May 2008
    Location
    IR, Iran
    Posts
    103

    std::getline and cin

    hi,
    I have this code:
    PHP Code:
    ...

    string BookNameAuthor;

    cout << "> Enter book title: ";
    getline(cinBookName);                        // this does not work

    cout << "> Enter author of book: ";
    getline(cinAuthor);                            // but this work!?

    ... 
    the output is:
    > Enter book title: > Enter author of book:
    and then waiting for something to write!

    I don't know what's wrong?
    Last edited by behzad_shabani; 06-08-2009 at 06:14 PM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Is that ALL of the code?

    I suspect you have something like:
    Code:
    int x;
    cin >> x;
    If so, you will need to get rid of the newline that was left behind by cin.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    May 2008
    Location
    IR, Iran
    Posts
    103
    This is part of a class method.
    BookName and Author are the attribute of the class!

    this is the class:
    PHP Code:
    class Book
    {
        public:
            
    Book();
            
    Book(string);

            
    void setBookInfo(void);

            
    friend void split(Book&, string, const char);

            
    string    BookName;
            
    string    Author;
            
    int        pages;
    }; 
    and this is setBookInfo declaration:
    PHP Code:
    void Book::setBookInfo()
    {
        
    cout << endl << "-------------------------------------------------------------------" << endl
             
    << "> Enter book title: ";
        
    getline(cinBookName);
        
    cout << "> Enter author of book: ";
        
    getline(cinAuthor);
        
    cout << "> Enter number of Book pages: ";
        
    cin >> pages;
        
    cout << "-------------------------------------------------------------------" << endl << endl;


  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    cin >> pages;
    Hey look! It's cin >> (some integer thing)! You can't mix >> and getline without thinking about it. In this case you may want to use cin.ignore() after the int, or use getline+strtol to read the int as well.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by tabstop View Post
    Code:
    cin >> pages;
    Hey look! It's cin >> (some integer thing)! You can't mix >> and getline without thinking about it. In this case you may want to use cin.ignore() after the int, or use getline+strtol to read the int as well.
    What prize do I get? Can I have a soft toy?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Registered User
    Join Date
    May 2008
    Location
    IR, Iran
    Posts
    103
    Quote Originally Posted by tabstop View Post
    Code:
    cin >> pages;
    Hey look! It's cin >> (some integer thing)! You can't mix >> and getline without thinking about it. In this case you may want to use cin.ignore() after the int, or use getline+strtol to read the int as well.
    I didn't understand, I don't think it's related to:
    Code:
    cin >> pages;
    because the first getline didn't work even when I remove cin >> pages; line

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by behzad_shabani View Post
    I didn't understand, I don't think it's related to:
    Code:
    cin >> pages;
    because the first getline didn't work even when I remove cin >> pages; line
    Is this getline the very first thing you do? You don't, say, have a menu where you have to type 1 to add a book or anything?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And there isn't another cin >> choice to select the "setBookInfo"? Like a menu for example?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    May 2008
    Location
    IR, Iran
    Posts
    103
    Quote Originally Posted by tabstop View Post
    Is this getline the very first thing you do? You don't, say, have a menu where you have to type 1 to add a book or anything?
    oh yeah!
    thanks, it works now
    thank you all

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    I'll just throw in something, too, to show my good intentions.
    First off myfunc(void) is the same as myfunc(), so you can drop the "void" part in your class.
    And secondly, you are stripping the names of your parameters to your functions. This is bad practice for a number of reasons. More info here: SourceForge.net: Do not remove parameter names - cpwiki
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    If you're mixing cin >> with getline, a good solution is to always call cin.ignore() after a call to cin >> (but never after a call to getline).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Evil
    By Elysia in forum C++ Programming
    Replies: 26
    Last Post: 06-26-2008, 07:20 AM
  2. Treating Types Of Data (Static Cast?)
    By shoover in forum C++ Programming
    Replies: 10
    Last Post: 06-11-2008, 02:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21