# int-2-string function

Printable View

• 05-12-2003
ichijoji
int-2-string function
Alright, so I'm making this game and I can't be passing all this num-with-string-with-num business around, so I've decided to simplify things with a rockin' int-to-string function so I could just pass one big string. I wrote this:

Code:

```string str; int length = 0; for (int temp = num; temp != 0; temp /= 10) ++length; for (int i = 1; i <= length; ++i) {         int temp = num % (i * 10);         if (i > 1) temp /= (i - 1) * 10;         str = char(temp + 48) + str; }```
but it doesn't work and it totally should. I don't need it to work in tricky situations like < 0 or == 0 or anything like that, I know what I'll be using it for. Does anybody have an idea as to what is wrong with this code? Also, I attached a simple prog that implements this if you want to see sample output.
• 05-12-2003
eMMeMM
Code:

```#include <iostream> #include <sstream> #include <string> int main() {   int myInt;   std::cout << "Enter an int: "; std::cin >> myInt;   std::stringstream ss; std::string s;   ss << myInt;    if (ss >> s) std::cout << "The string: " << s; else std::cout << "Something wrong ..."; }```
• 05-12-2003
JamesMI
I'm not sure what is wrong with your code (haven't really looked yet). If all you want to do is convert a number to a string you could do the following:

Code:

```#include <iostream> #include <cstdlib> #include <string> using namespace std; int main() {         int num;         char buffer[256];         string str = "";         cout << "Input num!\n>";         cin >> num;         str = itoa(num,buffer,10);                 cout << endl << str << endl << num << endl;         system("pause");         return 0; }```
I had to create a temporary buffer because the itoa() function wants one, i'm not sure if you can use it directly with the string class or not. I couldn't find a way to get it to work. I'm sure somebody else may have the answer to that one.

James
• 05-13-2003
Magos
Quote:

int temp = num % (i * 10);
I'm not sure what your attempting here, but doing modulus with 10, 20, 30 etc... seems wrong.
Perhaps you mean pow(10, i) (10 to the power of i, givning 10, 100, 1000 etc...)?
• 05-13-2003
JamesMI
I looked over you code again (I was bored), and changed it to:

Code:

``` #include <iostream> #include <string> #include <cstdlib> using namespace std; int main() {     string str;     int number;         cout << "Enter a number: ";     cin >> number;     cout << endl;         for (int temp = number; temp != 0; temp /= 10)     {              str = char( (temp%10) + 48) + str;     }         cout << "as an int:    " << number << endl;     cout << "as a string: " << str << endl;             system("pause");     return 0; }```
that seems to work ok for me.

James
• 05-13-2003
zMan
sprintf
let's not forget about the power of sprintf

int i = 10;
char szNumber;

sprintf(szNumber, "%d", i);

will result in storing "10" in szNumber
• 05-13-2003
Brighteyes
>will result in storing "10" in szNumber
Not until you make szNumber an array or a pointer with memory allocated to it. ;)
• 05-13-2003
PJYelton
Yeah, Magos is right. You want the power of ten, not a multiple of ten. Change the lines to:
Code:

```        int temp = num % pow(10,i);         if (i > 1) temp /= pow(10,i-1);```
• 05-13-2003
ichijoji
i see, thx for the help!
• 05-13-2003
golfinguy4
Re: sprintf
Quote:

Originally posted by zMan
let's not forget about the power of sprintf

int i = 10;
char szNumber;

sprintf(szNumber, "%d", i);

will result in storing "10" in szNumber

Only use this if you are not using cout.

Check the faq. The answer can be found there.;)