Converting an interger to a String?

This is a discussion on Converting an interger to a String? within the C++ Programming forums, part of the General Programming Boards category; I am new to programming in C++ and I am trying to convert an Integer to a string. would the ...

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    2

    Converting an interger to a String?

    I am new to programming in C++ and I am trying to convert an Integer to a string. would the function c_str work?

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    No.

    Click Here There should be a couple links that will answer your question.

    but .c_str() is a string fxn that converts a std::string to a c string.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    29
    Code:
    #pragma hdrstop
    #include <condefs.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(void)
    {
       int number = 12345;
       char string[25];
    
       itoa(number, string, 10);
       printf("integer = %d string = %s\n", number, string);
       getchar();
       return 0;
    }

    Hi I'm using CBuilder4 and found this code in the help!
    Hope it helps,
    Colin

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>I'm using CBuilder4 and found this code in the help!
    That's all C code, and non-standard at that.

    Like alpha said, the answer is in the FAQ.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    29
    He's New, I'm New.....
    I've asked for and received great help in the past. I read this site everyday and if there is something I can do to help someone I will. I am now getting a bit sick of the replys I get for the Super Dooper Moderators after I offer a suggestion. As I said I'm New to this and I'm only trying to help where I can.
    Thanks for the boost of confidence Hammer.
    Signing off, won't be back...
    Colin.

  6. #6
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,831
    Colin:

    Calm down!

    What Hammer said is correct. The questioner is asking for C++ help, giving him printf() is likely to confuse, as he may not have covered legacy code, as you yourself have pointed out, he is new.

    You did, also, give a compiler specific solution, which again might confuse and discourage a newcomer.

    >>> getting a bit sick of the replys I get

    I don't know which other threads you are referring too, but Hammers reply to this question is entirely correct, the answer is explained in the FAQ, and the code sample you have provided is not a general solution.

    >>> Signing off, won't be back...

    In which case I guess typing this was a waste of time...
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  7. #7
    Registered User
    Join Date
    Jan 2003
    Posts
    37
    Colin you are not new to this. You have been trying to program for 4 years now.
    Come on. Stop complaining.

  8. #8
    Registered User
    Join Date
    Apr 2003
    Posts
    2

    Smile

    Thank you for the valuable information. I was trying to figure it out and I did not know about the FAQ section. Thanks for reminding me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 07:02 AM
  2. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  3. string converting upper/lower
    By jlamn in forum C Programming
    Replies: 9
    Last Post: 09-24-2002, 07:01 PM
  4. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21