Thread: Integer to string?

  1. #1
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379

    Integer to string?

    Ok - I'm trying to turn an integer into a string.

    Say I have 203. I want it to become "203". I cant really think of any way to do this. My first idea was disasembling the integer and take it byte by byte. But I'm not quite sure how to do that, so my next idea was just say

    Code:
    char test=2238;
    string the;
    
    the+=test;
    But it ends up as some odd letter, it probably just cycles through 255 until it finds the value. (255, 510, 765, ect)

    I looked online, it gave me some libraries, but no code to solve it. I thought the idea was simple enough - I guess not.

    So, is their an easy way to cast an integer to a string? Vice versa is much easier, as you can dissasemble a string.
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You can use stringstreams with #include <sstream>
    e.g.
    Code:
    int num = 203;
    std::string str;
    std::stringstream ss;
    ss << num;
    ss >> str;
    //now str contains "203"
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    60% Braindead
    Join Date
    Dec 2005
    Posts
    379
    Ah, thank you!
    Code:
    Error W8057 C:\\Life.cpp: Invalid number of arguments in function run(Brain *)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  2. Conversion of character string to integer
    By supaben34 in forum C++ Programming
    Replies: 3
    Last Post: 10-30-2003, 04:34 AM
  3. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM