# Strings (C++) (Solved)

This topic is 4762 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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]

##### Share on other sites
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

##### Share on other sites
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

##### Share on other sites
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.

##### Share on other sites
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).

##### Share on other sites
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!

##### Share on other sites
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.

##### Share on other sites
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 );

##### Share on other sites
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?

1. 1
2. 2
Rutin
19
3. 3
JoeJ
16
4. 4
5. 5

• 35
• 23
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631701
• Total Posts
3001810
×