Thread: Creating Strings from other stuff

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    13

    Creating Strings from other stuff

    Simple newbie question:

    I have String variables.

    I have int variables.

    I want to combine them into a String that I can output to a TextBox or Label.

    How do I turn an int into a String? (talking about __gc Strings here)
    "Why not go mad?"

  2. #2
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Do you mean you wnat to display "The answer is 4" where 4 is a variable??

    printf("The answer is %d", x);

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    13
    Sort of, except I'm working with C++.net and I want to display the final String in the ->Text of a TextBox. It doesn't like it when I try to put anything other than a String (like the managed kind) in there.
    "Why not go mad?"

  4. #4
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    You mean, convert a int into a char array? look into the functions itoa [integer to a] (dont ask me what a is), and atoi.
    (e.g.)
    Code:
    char k[5];
    int x = 20;
    atoi(x, k, 10);
    I might have x and k backwards, but thats the idea. Now k has the string-version of x in it (the last parameter is the base for the conversion. I have it in base 10, 'cuz thats how most people count...). You can then use scrcat() to append it to another string, which should be what you're trying to do if I understand you right. Only warning is, make sure you make k big enough to hold all the digits!
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  5. #5
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    Probably the best way is to use sprintf.
    Code:
    #include <iostream>
    #include <cstdio>
    using namespace std;
    
    int main()
    {
            char new_string[100];
    
            sprintf( new_string, "%s: %d", "An integer", 123 );
            cout<< new_string <<endl;
    
            return 0;
    }
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

  6. #6
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    IMO the best way is to use stringstream.
    Then the type-checking is done automatically compile-time, not run-time as with printf.

    Code:
    #include <sstream>
    using namespace std;
    
     stringstream str;
    
     str << "An integer : " << 123;
    
     string myString = str.str();
    Last edited by Sang-drax; 09-10-2002 at 04:15 PM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  7. #7
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Originally posted by Hunter2
    You mean, convert a int into a char array? look into the functions itoa [integer to a] (dont ask me what a is), and atoi.
    (e.g.)
    Code:
    char k[5];
    int x = 20;
    atoi(x, k, 10);
    I might have x and k backwards, but thats the idea. Now k has the string-version of x in it (the last parameter is the base for the conversion. I have it in base 10, 'cuz thats how most people count...). You can then use scrcat() to append it to another string, which should be what you're trying to do if I understand you right. Only warning is, make sure you make k big enough to hold all the digits!
    Prelude posted something regarding atoi/atof, and the reasons why using strtod() and similar functions are a much better choice. You can do a search for it... I could explain it but it's explained nicely in the post.

    It's blind faith... anything she says I believe. (This goes for the other super-smart people too)

  8. #8
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Hmm... Never heard of strtod(). Guess I'll look into it though, since you mentioned it I only told what I knew, when I said atoi()...
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting the number of strings in a STRINGTABLE resource
    By eth0 in forum Windows Programming
    Replies: 1
    Last Post: 09-30-2005, 02:57 AM
  2. creating menus and other stuff
    By C+noob in forum Windows Programming
    Replies: 4
    Last Post: 07-13-2005, 07:20 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. c-style string vs. c++-style strings
    By Mbrio in forum C++ Programming
    Replies: 3
    Last Post: 02-10-2002, 12:26 PM
  5. array of strings + more
    By null in forum C Programming
    Replies: 10
    Last Post: 10-01-2001, 03:39 PM