Thread: number to string

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    36

    number to string

    I am having trouble finding how to do this properly.

    I know there is a command atoi, but it changes a string to a number.

    Im looking for a function(if there exists) to change/cast a number so it can be read as a string

    ex.

    5 = "5"
    5.5 = "5.5

    also, if there's any c++ reference website you can point me to for stuff such as this, it would be appreciated.(The C one is good, but need with string functions)

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    How about itoa?
    It's not ANSI C though...
    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.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    36
    well somewhat, although hard for me to find good documentation on it.

    Plus, it wouldnt work for a double

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    you could try sprintf() or stringstreams, both of which are ANSI standard.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    36
    trying sprintf() I dont really understand the function

    int sprintf(char*, const char*, ...)

    although I want to put it into a string, isnt a string just a char pointer? Although I am receiving an error for that.

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    char Buffer[1024];
    
    char* MyName = "Peter Pan";
    int MyAge = 28;
    
    //Create the string
    sprintf(Buffer, "Hello, my name is %s and I'm %d years old!", MyName, MyAge);
    
    //Print the string
    printf("%s", Buffer);
    You need a buffer, where the data will be stored. It have to be an array, not just a pointer.
    Then you specify a formatting string, and eventually some variables. Pretty much like printf().
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  2. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM