🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: att.lang.c++
Article #157

Re: cin.getline problem

#157
From: Fredrik Jangvik
Date: Wed, 03 Feb 1999 00:00
99 lines
3125 bytes
This is a multi-part message in MIME format.
--------------6E030009BAB1C60CC71804C5
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit

Hi again Tony. Well, I tried too and it didn�t work like you said, so I
tried an other code. And well, when I tried it worked. So try this code
instead.

#include <iostream.h>
int main()
{
char first[10];
char second[10];

for(int i=1; i<3;i++)
{
cout << "enter first name ";
cin.getline(first,10);
cin.clear();
cin.ignore('\n',10);
cout << "\nenter second name ";
cin.getline(second,10);
cin.clear();
cin.ignore('\n',10);
}
}

/Fredrik

tony wrote:
> 
> Hi Fredrik
> Thanks, tried that still doesn't work. It only occurs when
> typing in more chars than string length? I thought getline only accepted the
> delimiter size ignoring the \n key.But the extra chars must be staying in
> the keyboard buffer.
> Heres the code:
> 
> int main()
> {
> char first[10];
> char second[10];
> 
> for(int i=1; i<3;i++)
> {
> cout << "enter first name ";
> cin.getline(first,10);
> cout << "\nenter second name ";
> cin.getline(second,10);
> }
> 
> Fredrik Jangvik wrote in message <36B74810.3A6B118F@telia.com>...
> >Hi Tony. Try this after the last line in the iteration.
> >cin.ignore('\n', 100)
> >
> >/Fredrik
> >
> >tony wrote:
> >>
> >> Hi
> >> Can anyone explain why this doesn't work?
> >>
> >> for(int i=1; i<3;i++)
> >> {
> >>     cout << "enter first name ";
> >>     cin.getline(first,10);
> >>    cout << "\nenter second name ";
> >>    cin.getline(second,10);
> >     cin.ignore('\n',100);   // Here that is.
> >> }
> >>
> >> assuming vars are set up correctly etc. On the second iteration
> >> it skips the first cin.getline but displays both couts and waits for
> >> entry of second name. Do i need to flush the buffer after each getline if
> so
> >> how?
> >> Thanks Tony
--------------6E030009BAB1C60CC71804C5
Content-Type: text/x-vcard; charset=us-ascii;
 name="jangvik.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for Fredrik Jangvik
Content-Disposition: attachment;
 filename="jangvik.vcf"

begin:vcard 
n:Jangvik;Fredrik 
tel;home:0525-29929
x-mozilla-html:FALSE
adr:;;Aff�rsv�gen 16;457 30 TANUMSHEDE;;;Sverige
version:2.1
email;internet:jangvik@telia.com
title:Student of system analysis
x-mozilla-cpt:;-1
fn:Fredrik  Jangvik
end:vcard

--------------6E030009BAB1C60CC71804C5--
Technical Details
Message-ID: <36B86310.F20570E9@telia.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <bo4t2.18070$%3.1222@wards> <36B74810.3A6B118F@telia.com> <k_Lt2.21218$%3.1550@wards>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII
X-Google-Thread: 10d4ba,d6adead9ee0f9d93
X-Google-Attributes: gid10d4ba,public
From: Fredrik Jangvik <jangvik@telia.com>
Subject: Re: cin.getline problem
Date: 1999/02/03
Message-ID: <36B86310.F20570E9@telia.com>
X-Deja-AN: 440196762
X-NNTP-Posting-Host: t1o21p29.telia.com
References: <bo4t2.18070$%3.1222@wards> <36B74810.3A6B118F@telia.com> <k_Lt2.21218$%3.1550@wards>
X-Accept-Language: sv,en
Content-Type: multipart/mixed; boundary="------------6E030009BAB1C60CC71804C5"
X-Complaints-To: abuse@internet.telia.com
Organization: Telia Internet Services
Mime-Version: 1.0
Newsgroups: att.lang.c++