Problems using strings and cin.getline

F

Fred H

Hi

I'm trying to write a more or less fool proof
method of getting lines of text from either
standard input (cin) or files (ifstream). It
needs to be fool proof for later use, since
I can be quite the fool ;-)

For both cin and ifsteram the method getline
is defined, and I kind of like it, since I can
specify a maximum number of characters to get.
But getline requires a pointer to a char array,
and I would really like to put the input into
a string. I know that I can make myself a char
array, and use that as a temporary buffer, and
then insert it into my string when I'm done, but
that would be too simple, and the FAQ tells me
that arrays are evil, and I agree :p

I was thinking that maybe something along the
lines of

string buf;
buf.reserver(LENGTH);
cin.getline(buf.fake_cstring_pointer,LENGTH)

would work, but this way of using a string is
kind of messy, so I figured there would be a
version of getline that could actually take a
reference to a string, and ask it for it's
capacity on its own, and use that as the max
characters to get, or something like that.

My point is: I haven't been able to find any
such getlinish function. So... HELP! :)

Cheers
-Fred
 
A

Attila Feher

Fred said:
Hi

I'm trying to write a more or less fool proof
method of getting lines of text from either
standard input (cin) or files (ifstream). It
needs to be fool proof for later use, since
I can be quite the fool ;-)

For both cin and ifsteram the method getline
is defined, and I kind of like it, since I can
specify a maximum number of characters to get.
But getline requires a pointer to a char array,
and I would really like to put the input into
a string.
[SNIP]

Use the getline global function, which takes a string and a stream.

http://www.dinkumware.com/manuals/reader.aspx?b=p/&h=string2.html#getline
 
F

Fred H

My point is: I haven't been able to find any
such getlinish function. So... HELP! :)

I told you I was a fool. I only tried std::cin.getline()
and not std::getline(). So of course I discovered that I
could use std::getline(cin,mystring) after I postetd my OP.

My appologies to the group :\

-Frde
 
A

Attila Feher

Fred said:
I actually figured that out after posting my OP, but thanks anyway :)

And yes, I feel pretty stupid right now :p

Ah. Be me for few days *then* you will know what it means to feel stupid.
:)
 
K

Kevin Goodsell

Fred said:
I told you I was a fool. I only tried std::cin.getline()
and not std::getline(). So of course I discovered that I
could use std::getline(cin,mystring) after I postetd my OP.

My appologies to the group :\

It's a good idea to provide an email address when posting on usenet. If
you had, I could email you this directly instead of posting it to the
group, where it's not topical and where you might not see it.

<OT>
MSVC6 has a bug in its std::getline implementation. Please see this page
for information and a fix:

http://www.dinkumware.com/vc_fixes.html
</OT>

-Kevin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top