Sign in to follow this  

Small problem with converting

This topic is 4255 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

I am trying to convert an int to a string for a small game I am making to learn SDL. As this question is asked three or four times aday, I looked through a few old posts and decided to go like:
//int Life;
//std::string Health;
//std::stringstream ss;

ss << Life;
Health = ss.str();
But, as this portion of code is repeated, 'Life' gets added to 'ss' instead of just placed there. So, I can fix this in two different ways: clearing 'ss', or doing something like:
ss = Life;
Health = ss.str();
How can I do either? I know nothing of stringstream.

Share this post


Link to post
Share on other sites
the 'str' member function is overloaded as a mutator as well as an accessor, so you can write 'ss.str("")'.

Alternately, use a different stream, or do the work in a function (so that a different stream is implicitly used for each function call).

Alternately, if you are trying to create one big string for output with all the bits of information in it - then just do that. Let the stringstream be your buffer for the whole output message, and append the numbers and also intervening text.

Share this post


Link to post
Share on other sites
There are different ways to do it, but I'll show you one. It's pretty cool.
[source#include <iostream>
#include <stdlib.h>

using namespace std;

int main()
{
int num = 100;
char buffer[10];

itoa(num, buffer, 10);

cout << buffer << endl;

num = 85;

itoa(num, buffer, 10);

cout << buffer << endl;
}
What it does is converts the number and places it in buffer. This way you dont even have to deal with strings (although you could always append it do the string).


[Edit] Dang!! beat by Zahlman. His is probably better, although itoa is something rather nifty in times. [/Edit]

Share this post


Link to post
Share on other sites
Quote:
Original post by MikeTacular
There are different ways to do it, but I'll show you one. It's pretty cool.
Perhaps, if by 'pretty cool' you mean non-standard and unsafe :-) See here for more info.
Quote:
This way you dont even have to deal with strings.
You should want to deal with std strings - in almost all cases they'll make your life easier, not harder.
Quote:
...but I'll definately look into the itoa thing, I'be seen it mentioned before.
I wouldn't bother - you're almost certainly better off using the C++ standard library.

Share this post


Link to post
Share on other sites

This topic is 4255 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this