delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/06/22/07:38:28

Newsgroups: comp.os.msdos.djgpp
From: manni DOT heumann AT gmx DOT de (Manni Heumann)
Subject: Re: getline problem
References: <QAl45.6554$_55 DOT 140617 AT news2-win DOT server DOT ntlworld DOT com> <3951edf4$1_1 AT news DOT uni-bielefeld DOT de>
X-Newsreader: News Xpress 2.01
Mime-Version: 1.0
Date: Thu, 22 Jun 2000 10:51:10 GMT
NNTP-Posting-Host: ppp36-6.hrz.uni-bielefeld.de
Message-ID: <3951efa1_1@news.uni-bielefeld.de>
X-Trace: 22 Jun 2000 12:51:13 +0200, ppp36-6.hrz.uni-bielefeld.de
Lines: 46
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Sorry, I made a little mistake here:

>>
>>cin.getline(forename,20);
>>/* then to empty buffer */
>>  if(strlen(forename)==19)
> Don't check the length of the buffer, check the state of the stream:
>   if (cin.fail())
>>  {
>and show that you caught the error:
>     cin.clear ();
>>    cin.get(ch);
This is an endless loop at the moment:
>>    while(ch != '\n')
>>      cin.get(ch);
>>  }

Get will never read '\n' because it skips the whitespace, but could 
could check for cin.eof().

Still:

>
>But this is all quite complicated. Why don't you forget about the char 
>array buffer and use a string instead?
>
>#include <iostream>
>#include <string>
>
>int main ()
>{
>   string buf;
>   getline (cin, buf);
>}
>
>That's it. The string will grow to the required length, and as long as 
>you are sure, that the line isn't endless, you won't have to care about 
>any buffer sizes.
>
>

--

Manni

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019