LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 09-06-2005, 04:40 PM   #1
blizunt7
Member
 
Registered: Mar 2004
Distribution: Fedora Core 1,2,3, RHEL3,4,5 Ubuntu
Posts: 272

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.


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

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

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

Rep: Reputation: 31
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:

01/01/2005<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, 07:32 PM   #3
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: LFS-Version SVN-20091202, Arch 2009.08
Posts: 1,485

Rep: Reputation: 66
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, 10:34 PM   #4
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 31
Quote:
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...
 
  


Reply


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 08:54 PM
"Disk boot failure. Enter system disk and press enter" Fear58 Linux - Hardware 3 06-25-2004 05:17 PM
Cant press enter for install B McHack Mandriva 4 01-22-2004 08:58 PM
Random windows open when I press enter in KDE pointdexter Linux - Software 0 11-12-2003 02:05 PM
press 'I' to enter interactive startup? xiaoxiao Linux - Software 3 04-06-2001 07:39 AM


All times are GMT -5. The time now is 05:05 AM.

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