How to convert int to string?

This is a discussion on How to convert int to string? within the C++ Programming forums, part of the General Programming Boards category; Is there a way to get a int into a string, so I can use it as a string? I ...

  1. #1
    Registered User jon_nc17's Avatar
    Join Date
    May 2002
    Posts
    15

    Question How to convert int to string?

    Is there a way to get a int into a string, so I can use it as a string? I tried typecasting... lol that didint work, any ideas?

    Thanks!
    My Compiler is Borland's C++ 5.01!

    Thanks,

    Jon Scott

  2. #2
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493

    Re: How to convert int to string?

    Originally posted by jon_nc17
    Is there a way to get a int into a string, so I can use it as a string? I tried typecasting... lol that didint work, any ideas?

    Thanks!
    just a word of caution:
    c++ doesn't natively support strings. it only deals directly with numbers. if your string looks something like this:
    PHP Code:
    chartemp "wpeokwpokerpow"
    you have a pointer address. typecasting will give you that address and convert it to int form.

    if your string looks like this:
    PHP Code:
    string temp "wpeokwpokerpow"
    then casting might work. but casting there could be an overloaded operator function, designed for that.

    sigh... can someone check if the stl string class does this?

  3. #3
    Registered User toaster's Avatar
    Join Date
    Apr 2002
    Posts
    161
    can't you just do the thing manually?
    this may have some errors since I didn't test it

    Code:
    ...
    int number,x,copy=0;
    cin>>number;
    copy=number;
    for(x=0;copy!=0;x++)
    {
     copy/=10;
     x++;
    }
    int *array = new int[x];
    copy=number;
    for(x=0;x<array.strlen();x++)
    {
     array[x]=copy%10;
     copy%=10;
    }
    ...
    // if you want to include the sign (example: '+' or '-'), you need to change the array to type char. otherwise, the sign will be lost because I can't think of another way.
    think only with code.
    write only with source.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to combine these working parts??
    By transgalactic2 in forum C Programming
    Replies: 0
    Last Post: 02-01-2009, 07:19 AM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 12:05 PM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-01-2003, 11:26 PM

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