Strings (C++) (Solved)
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]
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
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
// Allmight
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).
Quote:Original post by snk_kidQuote: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!
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.
Quote:Original post by SirLuthorQuote:Original post by snk_kidQuote: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 );
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement