# Strings (C++) (Solved)

## Recommended Posts

fatnickc    122
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]

SirLuthor    364

##### Share on other sites
Conner McCloud    1135
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
Allmight    206
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
snk_kid    1312
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
fatnickc    122
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
SirLuthor    364
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
fatnickc    122
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
MaulingMonkey    1730
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
fatnickc    122
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?

##### Share on other sites
SirLuthor    364
Quote:
 Original post by fatnickcSurely, 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?

I'm not sure I understand your question? std::getline will read an input stream, be it std::cin, some open file, or whatever, into an std::string. Or are you asking about the scope resolutions? You can do as MaulingMonkey pointed out, or if you're using namespace std; (which is a bad idea in general, because it negates all the benefits that namespaces were meant to give, that is, avoiding name clashes, and being able to avoid polluting the global namespace.), you can do this:

string temp;getline(cin, temp);

MaulingMonkey: Yea, I just pulled the declarations straight from my handy reference [smile]

##### Share on other sites
fatnickc    122
Ok, but where does the inputted stuff go in your example. Right now, I use
cin.getline(written, 100);

to use cin with getline, putting the inputted stuff in written, with a maximum of 100 characters.
In your one, I can see how you tell the compiler you are using cin, and where it goes, but don't you need to have something to break input after, say, 100 characters?

##### Share on other sites
MaulingMonkey    1730
Quote:
 Original post by fatnickcOk, but where does the inputted stuff go in your example. Right now, I usecin.getline(written, 100); to use cin with getline, putting the inputted stuff in written, with a maximum of 100 characters.In your one, I can see how you tell the compiler you are using cin, and where it goes, but don't you need to have something to break input after, say, 100 characters?

Nope, it'll read up to the newline character, growing the string to a larger one as needed.

This is because the free function takes a reference to a std::string, which can be safely resized via std::string::resize, rather than a random pointer to memory as std::istream<...>::getline does, which could point to part of a C-style array, new[]ed memory, malloced memory, or std::allocator< char >::allocate d memory, and is thus not safely resizeable.

##### Share on other sites
SirLuthor    364
Quote:
 Original post by fatnickcOk, but where does the inputted stuff go in your example. Right now, I usecin.getline(written, 100); to use cin with getline, putting the inputted stuff in written, with a maximum of 100 characters.In your one, I can see how you tell the compiler you are using cin, and where it goes, but don't you need to have something to break input after, say, 100 characters?

Note that though it breaks by default at the newline character, you can change that, but calling it like this, or with whatever character you would like to break at as the third argument.
std::getline(std::cin, someString, ';')

##### Share on other sites
fatnickc    122
Great, thanks! I understand what to do now, and can see how to expand upon these ideas.

However, is there any way to get it to store the last character (i.e., in the previous example, ;)? It would be useful for me because when reading back from the file i store the string in, I want an easy break point.

##### Share on other sites
fatnickc    122
Anyone? At the moment, I'm simply saying the usr must end the program by typing '~;', then the getline stops at ;, so when reading from file I stop at ~. However, this is a little impractical, so is there a way to also store the ending character (i.e. ;)?

##### Share on other sites
Sneftel    1788
Why not just append ';' to the string?

##### Share on other sites
illone    151
...or enclose all strings in single/double quotes, as in:

"hello world"

even if this string is on two lines like:

"hello
world"

you will still know that this should be one string.
this way you can have mutiple items (numbers, strings)
on the same line, like:

"bobby johnson" "entry programmer" 111-22-3333 444-5555 31,000 "not too happy"

or whatever. all strings are clearly distiguishable to
your program, and all other fields should have a clearly defined
format to follow. two strings being directly adjacent to
one another (even on the same line) would be no problem to
handle.

ill++

##### Share on other sites
fatnickc    122
Not quite sure what you mean (well how it would help me) illone. However, Sneftel, yours should be great. Thanks.