Thread: int-2-string function

  1. #1
    GA ichijoji's Avatar
    Join Date
    Nov 2002
    Posts
    179

    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.
    Illusion and reality become impartiality and confidence.

  2. #2
    Registered Luser
    Join Date
    Apr 2003
    Posts
    17
    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 ...";
    }
    Last edited by eMMeMM; 05-12-2003 at 11:24 PM.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    16
    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

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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...)?
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    16
    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

  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    223

    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
    zMan

  7. #7
    Open to suggestions Brighteyes's Avatar
    Join Date
    Mar 2003
    Posts
    204
    >will result in storing "10" in szNumber
    Not until you make szNumber an array or a pointer with memory allocated to it.
    p.s. What the alphabet would look like without q and r.

  8. #8
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    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);

  9. #9
    GA ichijoji's Avatar
    Join Date
    Nov 2002
    Posts
    179
    i see, thx for the help!
    Illusion and reality become impartiality and confidence.

  10. #10
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138

    Re: sprintf

    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  3. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM