Strings (C++) (Solved)

Started by
17 comments, last by fatnickc 18 years, 9 months ago
How come a string is broken when a space is encountered? For instance When using cin, you have to use three strings to take in, for example, 'Fatnickc is cool'. When I'm reading from a file, because of what I have to do with the data collected, Iuse a string. However, as I don't know how many spaces there are, I don't know how many strings to cater for. So, I make a lot. However, when you cout these, they make seemingly random symbols, which look ugly. Not on the right computer now, but I think the solution is to strcpy '/0' to them before the fin. I'll be back.. [Edited by - fatnickc on July 2, 2005 10:43:26 AM]
---------------------------------Did I hear someone say peanuts?
Advertisement
std::istream::getline()
Free speech for the living, dead men tell no tales,Your laughing finger will never point again...Omerta!Sing for me now!
It needs to break somewhere, and whitespace is a perfectly reasonable place. If you want to use something else, there's the function get(), which can operate on stringbufs. I've never used it, but it seems like a good starting point.

CM
As SirLuthor pointed out, getline is your sollution. It is ment to read text until it encounters a line break, while cin only reads text until it encounters a whitespace.

// Allmight
-------------------------------------------------Founder and DirectorAllSoft Studios
Quote:Original post by SirLuthor
std::istream::getline()


That only works for C-style strings though, for std::basic_string there is version of getline for it but its a free-function not member function.
Thank you very much. My problems are solved, and also it has made something else much quicker (printing to a new line when reading from files which have new lines).
---------------------------------Did I hear someone say peanuts?
Quote:Original post by snk_kid
Quote:Original post by SirLuthor
std::istream::getline()


That only works for C-style strings though, for std::basic_string there is version of getline for it but its a free-function not member function.

Oh, I thought he was using c-strings... Guess I didn't read his post well enough..

Anyway, the function you want to read into a string is, if I recall correctly, something like this:

template<class E, class T, class A>
basic_istream<E, T>& getline(basic_istream <E, T>& is, basic_string<E, T, A>& str);

Or if you want to change the character that it stops on:

template<class E, class T, class A>
basic_istream<E, T>& getline(basic_istream <E, T>& is, basic_string<E, T, A>& str, E delim);


Cheers!
Free speech for the living, dead men tell no tales,Your laughing finger will never point again...Omerta!Sing for me now!
Okay, thanks. Previously, I was using a mix of C style strings and new ones, because of what I was doing to some of the strings. However, the ones i needed to use the function on were C-style, so it wouldn't have mattered, but now I can use new strings as well/instead.
---------------------------------Did I hear someone say peanuts?
Quote:Original post by SirLuthor
Quote:Original post by snk_kid
Quote:Original post by SirLuthor
std::istream::getline()


That only works for C-style strings though, for std::basic_string there is version of getline for it but its a free-function not member function.

Oh, I thought he was using c-strings... Guess I didn't read his post well enough..

Anyway, the function you want to read into a string is, if I recall correctly, something like this:

template<class E, class T, class A>
basic_istream<E, T>& getline(basic_istream <E, T>& is, basic_string<E, T, A>& str);

Or if you want to change the character that it stops on:

template<class E, class T, class A>
basic_istream<E, T>& getline(basic_istream <E, T>& is, basic_string<E, T, A>& str, E delim);


Cheers!


It's simple enough to use:

std::string line;std::getline( std::cin , line );
Surely, however, using std namespace as I do, writing string to make a new one would make the new-style ones, right? Just the way you cin differs, right?
---------------------------------Did I hear someone say peanuts?

This topic is closed to new replies.

Advertisement