Article View: att.lang.c++
Article #158Re: cin.getline problem
From: tony
Date: Fri, 05 Feb 1999 00:00
Date: Fri, 05 Feb 1999 00:00
81 lines
2735 bytes
2735 bytes
Hi again
Ok thanks that worked but, the user needs to
press return twice to get rid of the cin.ignore chars
not really an effective solution still need to find out why the buffer isn't
flushed i think?
Fredrik Jangvik wrote in message <36B86310.F20570E9@telia.com>...
>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
Technical Details
Message-ID:
<8LHu2.1567$kZ.135@wards>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<bo4t2.18070$%3.1222@wards> <36B74810.3A6B118F@telia.com> <k_Lt2.21218$%3.1550@wards> <36B86310.F20570E9@telia.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII X-Google-Thread: 10d4ba,d6adead9ee0f9d93 X-Google-Attributes: gid10d4ba,public From: "tony" <tony@netuk.force9.co.uk> Subject: Re: cin.getline problem Date: 1999/02/05 Message-ID: <8LHu2.1567$kZ.135@wards> X-Deja-AN: 441097866 References: <bo4t2.18070$%3.1222@wards> <36B74810.3A6B118F@telia.com> <k_Lt2.21218$%3.1550@wards> <36B86310.F20570E9@telia.com> X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3110.3 X-Complaints-To: abuse@plus.net.uk X-Trace: wards 918243396 212.56.92.237 (Fri, 05 Feb 1999 19:36:36 BST) NNTP-Posting-Date: Fri, 05 Feb 1999 19:36:36 BST Newsgroups: att.lang.c++