delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2000/06/22/06:47:25

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>
X-Newsreader: News Xpress 2.01
Mime-Version: 1.0
Date: Thu, 22 Jun 2000 10:44:03 GMT
NNTP-Posting-Host: ppp36-6.hrz.uni-bielefeld.de
Message-ID: <3951edf4$1_1@news.uni-bielefeld.de>
X-Trace: 22 Jun 2000 12:44:04 +0200, ppp36-6.hrz.uni-bielefeld.de
Lines: 48
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

If getline encounters a line that is larger than your buffer, the 
stream (cin in that case) will no longer be in a valid state . You can 
always check this with cin.fail(). If you then try to read further from 
the stream, it won't return anything useful. First you will have to 
acknowledge, that you checked the stream and that you saw it was no 
longer good. You do that with cin.clear().

Thus:
 
"Nigz" <n DOT bachmannNO AT SPAMcardiff DOT govOK DOT uk!> wrote:
>I am using rhide & have the following segment of code:
>
>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);
>    while(ch != '\n')
>      cin.get(ch);
>  }
>

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