Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 09-06-2005, 05:40 PM   #1
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 274

Rep: Reputation: 30
getline, why press enter twice??

Hey all,
I have been using getline without many problems for a while, sometimes (i dont know) i must include cin.ignore(1) before the getline so that the getline is not skipped over.

Now however almost the opposite problem, with the cin.ignore(1) , my getline works, but i must press enter untill the char is read, and without the cin.ignore(1), the getline still works, but the cursor is moved to the next line after the getline on the terminal.

    cout << "When was the item shipped (MM/DD/YYYY): ";
    cin >> dateSent; cout << endl;

    cout << "First Name only: ";
    cin.getline(first, 80);
    cout << endl;
Am i doing something wrong here?? thank you so much

Old 09-06-2005, 05:58 PM   #2
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
The problem comes from mixing cin << something with cin.getline. Because it is usually buffered, none of your input goes into the cin buffer until you press enter. So, taking your example above, say you enter:


All of that is put into the input buffer. Now when you do cin >> dateSent, 01/01/2005 is removed from the buffer and put into dateSent, leaving the <enter> character(s).

Now, say you do a cin.getline without first doing an ignore. Because getline will read the entire line up to the maximum length, or until it finds an <enter> it will immediately return a blank line because the <enter> is still left over from your cin >> dateSent!

Now, it gets even more confusing when you consider that in Windows, the <enter> is usually a sequence of not 1, but 2 characters. (One for carriage return (0x0D) and one for line feed (0x0A), often denoted by CRLF) Linux typically just has a sequence of 1 character. (LF) So in this case, for Windows you might actually have to ignore not 1, but 2 characters before your getline() will work properly.

Another option would be to just make a call to getline() before the one you actually want.

Hope that helps explain what is happening a bit...
Old 09-06-2005, 08:32 PM   #3
Senior Member
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
i dont mean to be an ass but this has been discussed many times in the past albeit about
c and not c++

and ill admit now ive had the same issue. Is got to do with buffers and such.

happy hunting.
Old 09-06-2005, 11:34 PM   #4
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
Originally posted by exvor
i dont mean to be an ass but this has been discussed many times.
As have many other questions. But nobody is forcing you to answer questions like this if you don't want to...


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
how do i scan a character from keyboard without the user having to press enter rasika Programming 6 02-11-2010 09:54 PM
"Disk boot failure. Enter system disk and press enter" Fear58 Linux - Hardware 3 06-25-2004 06:17 PM
Cant press enter for install B McHack Mandriva 4 01-22-2004 09:58 PM
Random windows open when I press enter in KDE pointdexter Linux - Software 0 11-12-2003 03:05 PM
press 'I' to enter interactive startup? xiaoxiao Linux - Software 3 04-06-2001 08:39 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:29 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration