Thread: turning real (number) variables into stirngs

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    731

    turning real (number) variables into stirngs

    I am pretty sure it is possible, but I am not sure how to turn a real variable into a string variable (using string.h).

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    I was given some of these links looking for the same thing:

    http://rafb.net/efnet_cpp/faq/conversions/int2string/
    http://www.parashift.com/c++-faq-lit....html#faq-39.1
    http://www.boost.org/libs/conversion/lexical_cast.htm

    And I was given some code that works similrly to atoi by another member:

    Code:
    void append_int(std::string & s, unsigned int n)
    {
        unsigned int tmp = 0;
    
        while (n) {
           tmp *= 10;
           tmp += n % 10;
           n /= 10;
        }
    
        while (tmp) {
            s.push_back('0' + tmp % 10);
            tmp /= 10;
        }
    }
    I wrote this assuming by <string.h> you meant <string> and std::string's. If you merely wanted char* strings, than you can use atoi() in <cstdio>

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Alright and yes I ment string not string.h. That first link you gave me worked perfectly!

    Thanks for passing along the help!

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    You could use stringstreams. Do a board search if you want more. They've been discussed several times recently. Just, use them as a normal stream when dealing with floating point numbers.

    And, atoi( ) goes the wrong way.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Damnit! itoa()

  6. #6
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    There is only a minor problem with that one:
    Portability.
    Not defined in ANSI-C. Supported by some compilers.
    Best sticking with:
    1. stringstream
    2. sprintf
    3. your own "home-brew" function

    With heavy preference for the first.

    Cheers
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  7. #7
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    This is in the faq...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  8. #8
    Banned
    Join Date
    Jun 2005
    Posts
    594
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    using namespace std;
    
    int main()
    {
    	int x = 10;
    	string s;
    	stringstream ss;
    	ss << x;
    	s = ss.str();
    	cout << s << endl;
    	cin.get();
    	return 0;
    }

  9. #9
    Registered User
    Join Date
    Nov 2004
    Location
    Slovenia, Europe
    Posts
    115
    I usualy do it like this:

    (string)(char*)variable
    [C++]
    IDE: DevC++ 4.9.9.2 (GCC 3.4.2)
    2nd compiler: g++ (GCC 3.4.3/4.0.0)
    3rd compiler: Borland 5.5
    [C#]
    IDE: Microsoft Visual C# Express 2005
    2nd IDE: SharpDevelop
    2nd compiler: csc in Command Prompt
    .NET Framework: 2.0
    [PHP]
    Core: 5.1.0 beta 3
    IDE: PHPEdit
    2nd IDE: Notepad
    Favourite extensions: exif,gd2,mysql
    Favourite PEAR packages: DB, XML_RSS, ID3
    Favourite databases: SQLite, MySQL

  10. #10
    Banned
    Join Date
    Jun 2005
    Posts
    594
    i dont think you have ever really used that...
    and had it work, at least if this is what your meaning
    by your post

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	int length = 10;
    	string mystring = (string)(char*)length;
    	cout << mystring << endl;
    	cin.get();
    	return 0;
    }

    cause it doesnt work.

  11. #11
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Quote Originally Posted by ILoveVectors
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    using namespace std;
    
    int main()
    {
    	int x = 10;
    	string s;
    	stringstream ss;
    	ss << x;
    	s = ss.str();
    	cout << s << endl;
    	cin.get();
    	return 0;
    }

    That is exactly what I am using.

  12. #12
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by Tonto
    Code:
    void append_int(std::string & s, unsigned int n)
    {
        unsigned int tmp = 0;
    
        while (n) {
           tmp *= 10;
           tmp += n % 10;
           n /= 10;
        }
    
        while (tmp) {
            s.push_back('0' + tmp % 10);
            tmp /= 10;
        }
    }
    Just make sure you don't use this function for any value of n that is ten digits long and ends in 4, 5, 6, 7, 8, or 9. [Some values that end in 4 work -- namely those with tens digit 1 or 0. Most ten-digit numbers that end in "24" also work -- but if the hundreds digit is 9, they might not. (If the reversed form of the number is 4294967296 or higher, the function breaks.)] Assuming that an int is 32 bits long.

    I'd go with using a char array as a stack instead of another int, next time around. So just use stringstreams :-)

  13. #13
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Check out wsprintf()

  14. #14
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    wsprintf is for use with wchar_t, not char. sprintf would generally be the appropriate function.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  2. %16 with double
    By spank in forum C Programming
    Replies: 11
    Last Post: 03-05-2006, 10:10 PM
  3. Number system base M, print numbers N digits wide...
    By biterman in forum C Programming
    Replies: 12
    Last Post: 11-19-2001, 04:31 AM
  4. Perfect number
    By TheSki in forum C++ Programming
    Replies: 2
    Last Post: 10-30-2001, 04:34 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM