Thread: Join string and int.

  1. #1
    Registered User xconspirisist's Avatar
    Join Date
    Nov 2003
    Posts
    44

    Join string and int.

    This is going to sound crazey, but I've no idea why this 'wont work'.

    Code:
    message = "value of x is " << x << " value of y is " << y;
    x and y are int's. The end result has to be a char.

    error C2296: '<<' : illegal, left operand has type 'char [14]'

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Hi,

    It looks like you took a statement like this:
    Code:
    cout<<"value of x is " << x << " value of y is " << y;
    and substituted message= in place of cout<<. What on earth did you read about C++ that made you think that cout<< and message= are equivalent?

    The fact that you are asking such a question indicates you probably wouldn't understand the somewhat complex method you have to use for joining a string and an int in C++, but if you are interested look up stringstreams.
    Last edited by 7stud; 06-11-2005 at 03:48 AM.

  3. #3
    Registered User xconspirisist's Avatar
    Join Date
    Nov 2003
    Posts
    44
    'message' is a char variable.

    Where x is 5, and y were 3, I'd like the string to read

    value of x is 5 value of y is 3.
    I assumed that << would join variables together. In php, you would write something like:

    Code:
    echo "value of x is " . $x . " value of y ix " . $y;
    Might I have some example code equivilent to that?

    >>>> EDIT

    I've solved the issue, is this a hacky solution?

    Code:
    	int x = 5, y = 3;
    	char message [50];
    
    	sprintf( message, "x is %i , y is %i ", x, y);
    Last edited by xconspirisist; 06-11-2005 at 04:34 AM.

  4. #4
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    operator << can take string and numeric right-hand arguments, but there must be a class on the left-hand side performing the operation (whatever that operation may be). In the example of
    Code:
    cout << "the value of x is " << x;
    it would be equivalent to writing
    Code:
    cout.operator<<("the value of x is ").operator<<(x);
    Don't confuse that dot with a PHP dot for concatenation. cout's operator<< implementation returns what you should think of as cout itself. So really that translation would also be equivalent to saying
    Code:
    cout.operator<<("the value of x is");
    cout.operator<<(x);
    Since you are posting in the c++ thread, a grand solution would be to take advantage of the ostringstream class (#include <sstream>). You can search the board for examples.
    For a more primitive solution, use sprintf().

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Last edited by anonytmouse; 06-11-2005 at 04:55 AM.

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by xconspirisist
    This is going to sound crazey, but I've no idea why this 'wont work'.

    Code:
    message = "value of x is " << x << " value of y is " << y;
    x and y are int's. The end result has to be a char.
    basicly you have
    primitive type A << primitive type B
    in C/C++, this operator, for primitive types, is only defined for integers (obviously), because it's a bitwise operator.
    So char[] <<int doesn't exist, and one can only overload an operator that has at least one class as argument.
    So, bad luck.
    What you what is this
    Code:
    string message("value of x is ");
    message = message + x +" value of y is " +y;
    Yes I separated in two lines because char* + int is char* but pointing wherelse

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> What you what is this

    I seriously doubt that is what the OP wanted. Using the operator+ on an int with a string does not add the string representation of that int to the string. A stringstream does that (as has been mentioned already). Instead, you will probably get an error, and if it did work it would add on the character whose code is equal to that int.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Replies: 26
    Last Post: 11-30-2007, 03:51 AM
  4. Game Won't Compile
    By jothesmo in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2006, 04:24 PM
  5. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM