Jump to content
  • Advertisement
Sign in to follow this  
fatnickc

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!