Convert an int to a char then concat it to a string.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DevInCode
    New Member
    • Apr 2008
    • 55

    Convert an int to a char then concat it to a string.

    I can't get this to work for the life of me.


    I expect this to work but it does not.

    Any help?


    int i = 1;
    int letter = 65;

    osstream ss;
    ss << i;
    ss << char(65);
  • whodgson
    Contributor
    • Jan 2007
    • 542

    #2
    Code:
    int i=1;
    string s1,s2;
        s1='i';
        s2=char(65); // or s2='A'
        cout<<s1 + s2;//prints 1A
    Is this what you had in mind?

    Comment

    • DevInCode
      New Member
      • Apr 2008
      • 55

      #3
      Yes, I got it sorted out basically the same way. I think I was focusing too hard on why strings aren't pure and simple in c++, but really they aren't all that difficult. Thanks for the input! :)

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Keep in mind that you cannot convert an int of 65 into a char of 65.

        If you do, you get A. To get 65 to need to convert to 2 chars. A 6 and a 5.

        Comment

        Working...