Thread: Numeric to string help

  1. #1
    StupidQuestion
    Guest

    Numeric to string help

    how would i go about assigning a numerical data type to a string/composite string, i am havig the following problem:
    i want to pass the following to the DrawCaption Api:

    int Score=1234;
    DrawCaption(...,"Your Score: " + Score ,...);

    but you have to pass a purely string varialbe so i tried this:

    int Score=1234;
    char ScoreText[] = "Your Score: ";
    ScoreText[8] = Score; //and i know this just passes the number to ascii character form
    DrawCaption(...,ScoreText,...);

    so as u can see no good, do i have to create my own class to do this or is there a pre built class for this, or am i just not using the correct syntax???
    any help would be great

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Use a string stream object.
    Code:
    #include <strstream>
    #include <iostream>
    #include <string>
    
    using std::string;
    using std::strstream;
    using std::cout;
    using std::endl;
    using std::ends;
    
    int main()
    {
        string FinalString;
        int Score = 1234;
        strstream TempString;
    
        TempString << "Your Score: " << Score << ends;
        FinalString = TempString.str();
        cout << FinalString << endl;
    
        return 0;
    }
    I think that should be correct and give you a sample of what to do. Should output "Your Score: 1234"
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    StupidQuestion
    Guest
    Thats great thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM