8 Replies - 1673 Views - Last Post: 01 November 2010 - 08:24 AM Rate Topic: -----

#1 FljpFl0p   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 58
  • Joined: 13-October 10

Something's wrong with my getline()

Posted 01 November 2010 - 08:03 AM

I don't know why but when I run the program, it just skips my getline function.
But if I copy and paste the same line so that it looks like:
getline(cin,title);
getline(cin,title);

The program skips only the first getline, the second getline runs fine.
Don't know what's the problem, so I hope you guys can help me figure it out.
Here's the code, I deleted all the unnecessary stuffs to make it easier for you guys to keep track.
#include <iostream>
#include <string> 

using namespace std;

const int MAX = 10;

class Book
{
    private:
                string title;                
    public:
                Book();
                void setTitle();
};                        

int main()
{
    int i, size;
 
    Book books[MAX];
    
    cout << "Enter the number of book entries: ";
    cin >> size;
    cout << endl;
   
    for (i = 0; i < size; i++)
    {		
		books[i].setTitle();
		
		cout << endl;
    }
}                    

Book::Book()
{

}

void Book::setTitle()
{
	cout << "\nEnter the book title: "; 
	getline(cin,title);
}



Is This A Good Question/Topic? 0
  • +

Replies To: Something's wrong with my getline()

#2 SarumanTheWhite   User is offline

  • D.I.C Regular

Reputation: 74
  • View blog
  • Posts: 352
  • Joined: 04-November 08

Re: Something's wrong with my getline()

Posted 01 November 2010 - 08:08 AM

Edit: Damn, disregard what I said. Was looking up the wrong getline

This post has been edited by SarumanTheWhite: 01 November 2010 - 08:17 AM

Was This Post Helpful? 0
  • +
  • -

#3 oscode   User is offline

  • D.I.C Regular

Reputation: 109
  • View blog
  • Posts: 257
  • Joined: 24-October 10

Re: Something's wrong with my getline()

Posted 01 November 2010 - 08:09 AM

Your cin >> size; is leaving a new line in the buffer.

cin >> size;
cin.ignore(500);



Should fix it, see http://www.cplusplus...istream/ignore/

This post has been edited by oscode: 01 November 2010 - 08:11 AM

Was This Post Helpful? 0
  • +
  • -

#4 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Something's wrong with my getline()

Posted 01 November 2010 - 08:16 AM

When you fetch numeric input from the user, 2 pieces of data are in the input buffer: the number you are after and a line terminator. When you extract the number, >>, the terminator is left in the buffer. You can 'fix' this by reading the terminator directly:

...
    cout << "Enter the number of book entries: ";
    cin >> size;
    cin.get();  // remove line terminator
    cout << endl;
...



As you noticed getline() will also work as it considers the terminator as an empty line.

View PostSarumanTheWhite, on 01 November 2010 - 07:08 AM, said:

getline looks like this:

istream& getline (char* s, streamsize n );



Looks like you're not using it correctly


No, the usage is correct. It's the string's library global function getline():

getline
Was This Post Helpful? 1
  • +
  • -

#5 FljpFl0p   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 58
  • Joined: 13-October 10

Re: Something's wrong with my getline()

Posted 01 November 2010 - 08:17 AM

Nope, doesn't seem to work at all. oscode's method sounds right but I ended up having to input a bunch of numbers til it got to the getline(), and the problem still remains.

Edit: ok, n8wxs's way fixed the problem. I never knew getline() would take the empty line too. Thanks a bunch guys.

This post has been edited by FljpFl0p: 01 November 2010 - 08:20 AM

Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Something's wrong with my getline()

Posted 01 November 2010 - 08:19 AM

View PostSarumanTheWhite, on 01 November 2010 - 08:08 AM, said:

getline looks like this:

istream& getline (char* s, streamsize n );



Looks like you're not using it correctly


There are two sets of getline() functions one works with Cstrings (which you know about) and one that works with std::string.

Please see the following links getline for Cstrings and getline for std::string.

Jim
Was This Post Helpful? 0
  • +
  • -

#7 oscode   User is offline

  • D.I.C Regular

Reputation: 109
  • View blog
  • Posts: 257
  • Joined: 24-October 10

Re: Something's wrong with my getline()

Posted 01 November 2010 - 08:22 AM

Oh sorry by default it ignores until EOF, not '\n'.

Try cin.ignore(500, '\n');

It is covered by the FAQ here

This post has been edited by oscode: 01 November 2010 - 08:23 AM

Was This Post Helpful? 1
  • +
  • -

#8 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Something's wrong with my getline()

Posted 01 November 2010 - 08:24 AM

View PostFljpFl0p, on 01 November 2010 - 08:17 AM, said:

Nope, doesn't seem to work at all. oscode's method sounds right but I ended up having to input a bunch of numbers til it got to the getline(), and the problem still remains.

Edit: ok, n8wxs's way fixed the problem. I never knew getline() would take the empty line too. Thanks a bunch guys.

If you use the second form of ignore adding the newline character as the delimiter it should work.

cin.ignore(500,'\n');


Jim
Was This Post Helpful? 1
  • +
  • -

#9 FljpFl0p   User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 58
  • Joined: 13-October 10

Re: Something's wrong with my getline()

Posted 01 November 2010 - 08:24 AM

This time oscode's method works. Thanks, now I have 2 methods to consider.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1