How to merge string and int? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

How to merge string and int?

chamsterchamster Posts: 662Member
I have a string object and an int variable.

Now i need the int to be connected to the string as in:

string s; int m = 123;
s = "sum " + m;
cout

Kind Regards
Konrad
----------------------------
(+46/0) 708-70 73 92
[email protected]
www.chamster.com

Comments

  • nimfritznimfritz Posts: 29Member
    : I have a string object and an int variable.
    :
    : Now i need the int to be connected to the string as in:
    :
    : string s; int m = 123;
    : s = "sum " + m;
    //This won't work, concatenation fo a string and an int is illegal
    : cout << s << endl;
    :
    : It's required to connect the whole thing to one string BEFORE the output operation.
    :
    : That's the way i'm used to in Java. In C++ i get a bunch
    : of "noise" which tells me that i print a part of memory
    : rather than string values. Is there a way to explicitly (or
    : implicitly) cast an int to string?
    :
    :
    : Kind Regards
    : Konrad
    : ----------------------------
    : (+46/0) 708-70 73 92
    : [email protected]
    : www.chamster.com
    :

    string s = "sum", i;
    int m = 123;

    i = atoi(m);
    cout<<s + i<<endl;

    Try that, I'm not very proficient with the String class and I don't know if you need to convert to a c_str before using atoi. But it's a start.
  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [code]
    int m = 123;

    i = atoi(m);
    [/code]
    I think you meant itoa().
    atoi() means Ascii to Integer and you passing integer and not string.
  • nimfritznimfritz Posts: 29Member
    : [code]
    : int m = 123;
    :
    : i = atoi(m);
    : [/code]
    : I think you meant itoa().
    : atoi() means Ascii to Integer and you passing integer and not string.
    :
    Ack, thanks. I was going on autopilot when I wrote that, guess I should pay more attention ;p
  • MatthewDMatthewD Posts: 297Member
    : : [code]
    : : int m = 123;
    : :
    : : i = atoi(m);
    : : [/code]
    : : I think you meant itoa().
    : : atoi() means Ascii to Integer and you passing integer and not string.
    : :
    : Ack, thanks. I was going on autopilot when I wrote that, guess I should pay more attention ;p

    ----------------

    I'd suggest using stringstream, since itoa is not standard.
    Try this:

    [code]
    #include string;
    #include sstream;

    //...//

    std::string myString("String#");
    int myInt =1;

    std::stringstream ss;
    ss << myString << myInt;
    std::cout << (ss.str());
    [/code]

    You can easily add a space or whatever else to the stringstream.
    For example:

    ss << myString << " " << myInt << ".";

    The function strinstream::str() returns a string based on the stream. The ol' stringstream is a very useful class.

    Matthew.

  • Eric TetzEric Tetz Posts: 2,141Member
    : : I think you meant itoa().
    : : atoi() means Ascii to Integer and you passing integer and not string.
    : :
    : Ack, thanks. I was going on autopilot when I wrote that, guess I should pay more attention ;p

    Just remember that itoa() is not a standard function, so you can't rely on it being there if you use a different compiler. You can use stringstream (as as already been shown), or sprintf...
    [code=ffffff]
    [color=000080]int[/color] number = [color=bb0000]5423[/color];
    [color=000080]char[/color] s[[color=bb0000]32[/color]];
    sprintf(s, [color=bb0000]"[/color][color=907050]%d[/color][color=bb0000]"[/color], number);
    [/code]
    Or, if you use a compiler that doesn't have 'itoa', you can use this one (note: it relies on std::reverse)...
    [code=ffffff]
    [color=000080]char[/color]* itoa ([color=000080]int[/color] val, [color=000080]char[/color] *buf, [color=000080]unsigned[/color] radix)
    {
    [color=000080]char[/color]* p = buf;
    [color=000000][b]if[/b][/color] (radix == [color=bb0000]10[/color] && val < [color=bb0000]0[/color])
    {
    *p++ = [color=bb0000]'-'[/color];
    val = -val;
    }
    [color=000080]char[/color]* firstDigitPos = p;
    [color=000000][b]do[/b][/color] {
    [color=000080]char[/color] digit = val % radix;
    [color=000000][b]if[/b][/color] (digit > [color=bb0000]9[/color])
    *p++ = digit - [color=bb0000]10[/color] + [color=bb0000]'a'[/color];
    [color=000000][b]else[/b][/color]
    *p++ = digit + [color=bb0000]'0'[/color];
    val /= radix;
    } [color=000000][b]while[/b][/color] (val > [color=bb0000]0[/color]);
    *p = [color=907050]''[/color];
    reverse (firstDigitPos, p);
    [color=000000][b]return[/b][/color] buf;
    }
    [/code]
    Cheers,
    Eric

  • chamsterchamster Posts: 662Member
    : [code]
    : #include string;
    : #include sstream;
    :
    : //...//
    :
    : std::string myString("String#");
    : int myInt =1;
    :
    : std::stringstream ss;
    : ss << myString << myInt;
    : std::cout << (ss.str());
    : [/code]
    :

    ----------------------

    My compiler doesn't recognize either sstring or stringstream. It's supposed to be a standard C++ compiler (BloodShed) so my guess is that i miss something. What could that be?



    Kind Regards
    Konrad
    ----------------------------
    (+46/0) 708-70 73 92
    [email protected]
    www.chamster.com

  • MatthewDMatthewD Posts: 297Member
    : My compiler doesn't recognize either sstring or stringstream. It's supposed to be a standard C++ compiler (BloodShed) so my guess is that i miss something. What could that be?
    :
    :
    :
    : Kind Regards
    : Konrad

    I don't know why your compiler doesn't have stringstream. Do you get an error when you try to "#include " ?? If so, it would seem that your compiler is seriously out of date (5 years or so).

    Matthew.


  • chamsterchamster Posts: 662Member
    Sorry to bother you. I must have read something wrong. What i tried was

    #include

    not

    #include

    My mistake...

    I use:

    string funk (int m)
    {
    char c[32];
    sprintf (c, "%d", m);
    string s = c;
    return s;
    }

    What do you think of it? WHat's the "%d"-thingy?


    : I don't know why your compiler doesn't have stringstream. Do you get an error when you try to "#include " ?? If so, it would seem that your compiler is seriously out of date (5 years or so).



    Kind Regards
    Konrad
    ----------------------------
    (+46/0) 708-70 73 92
    [email protected]
    www.chamster.com

Sign In or Register to comment.