Sign in to follow this  

Converting a integer to a string object.

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

Okay, I have search and searched for a way to convert to a string object, so that I can push a integer value onto a string-based Queue. I have my program and it works great for strings, but I need a method to conver from int text to string texta. I have looked at Itoa, but I cannont make sense of the proto type. My compilers also likes to think that Itoa doesn't exist so.... some one help please? Here is the function and overloaded interger function
void Add(int text, int color)
       {
            char* texta=new char;
          itoa(text,texta,2);
            Mlist.push(texta);
            Clist.push(color);
       }

       
       void Add(string text, int color)
       {
                  
                  Mlist.push(text);
                  Clist.push(color);
       }

Share this post


Link to post
Share on other sites
Quote:
Original post by Plasmarobo
Okay, I have search and searched for a way to convert to a string object, so that I can push a integer value onto a string-based Queue. I have my program and it works great for strings, but I need a method to conver from
int text
to
string texta.
I have looked at Itoa, but I cannont make sense of the proto type. My compilers also likes to think that Itoa doesn't exist so.... some one help please?


C++:

#include <sstream>
#include <string>

std::stringstream ss;

ss << something_made_of_something_including_integers_chars_whatever;
std::string something_else;
something_else = ss.str(); // #1
// OR
ss >> something_else; // #2

Share this post


Link to post
Share on other sites
There have a been a few threads on this recently, so you might look through the first few pages of this forum for more detailed info. The short answer is, look into std::stringstream (or boost::lexical_cast<>). An example:
std::stringstream myStream;
std::string myString;
myStream << myInt;
myStream >> myString;

Share this post


Link to post
Share on other sites
itoa() is nonstandard, which is why your compiler probably doesn't think it exists (or you are including the wrong header). In any case, other posters have provided better solutions already.

Share this post


Link to post
Share on other sites
For simple conversions, boost::lexical_cast will rock your world:

int number = 5;
std::string numberStr = boost::lexical_cast<std::string>(number);
// numberStr = "5"

Share this post


Link to post
Share on other sites
okay, it does support Itoa, but I get the same
10101010101110101 output

I modded it to use:

void Add(int text, int color)
{
string texta = boost::lexical_cast<std::string>(text);
Mlist.push(texta);
Clist.push(color);
}


void Add(string text, int color)
{

Mlist.push(text);
Clist.push(color);
}


But I still get 10101010101

Share this post


Link to post
Share on other sites
Print the integer out to the console or to a file where you can see it just using printf, or fprintf.

printf("\n text: %d \n", text);

If it still shows up as a bunch of 1's and 0's, then the number is probably a bunch of 1's and 0's. Still a decimal number though.

Quote:
Original post by Plasmarobo
It will print the number, but in a weird
10101011101010100101 thing, I think that the memory adress is printing.

Generally, a memory address will print either as a hex number, or if you are excplicitly printing as a decimal as some ridiculously large (or small depending on your view) negative number. That's if you're using signed integers. If you're using unsigned, it'll show up as a really huge positive number.

Share this post


Link to post
Share on other sites
perhaps try making your own function?

an integer only reaches the billions or so (10^9). have a loop that takes the number and converts it by starting at 1 and ending at 9...

Than to get a specific number from the number you have to do something like this:

on the number 569 you'd first do 569%10 = 9, first number, than ((569-9)%100)/10=6, than ((569-60-9)%1000)/100=5 and so on if its a greater number.

than it'd be simple as appending the character to the return string.

Share this post


Link to post
Share on other sites

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