Mail Archives: djgpp/2000/06/22/07:38:28
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 -