Mail Archives: djgpp/2000/06/22/06:47:25
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 -