Results 1 to 7 of 7

Thread: integer and string concatenation

  1. #1

    Thread Starter
    Frenzied Member SomethinCool's Avatar
    Join Date
    Jan 2001
    Location
    Malvern, PA
    Posts
    1,407

    integer and string concatenation

    If I wanted to concatenate an integer with a string, what would the proper syntax?

    For a comp sci class, I'm working on my 4th assignment, and I forget how to do this (i'm not asking for you to do my assignment..i just would like some help because I'm stuck. I'm not a C++ programmer. I just know basic C++.)

    This is the line I have:
    orderArray[x] = lbsPeanuts + " Pounds of Peanuts";

    It's giving me compile errors and it is trying to add instead of concatenating lbsPeanuts to " Pounds of Peanuts".

    Anyone know how to do this? Thanks.

  2. #2
    Lively Member
    Join Date
    Jan 2004
    Posts
    82

    Re: integer and string concatenation

    Code:
    #include <iostream>
    #include <sstream>
    
    using namespace std;
    
    int main()
    {	
    	int integer = 123;
    
    	stringstream strstream;
    	strstream << "Integer: " << integer;
    
    	cout << strstream.str() << endl;
    
    	system("pause");
    }
    Use a stringstream.
    There are other ways but this is the C++ way.

  3. #3

    Thread Starter
    Frenzied Member SomethinCool's Avatar
    Join Date
    Jan 2001
    Location
    Malvern, PA
    Posts
    1,407

    Re: integer and string concatenation

    Is there anything more basic then that? I would use it, but for our class we only got to arrays. I'm in a cmpsc 101 class. All we did was just go from learning iostream and cout statements to two-dimensional arrays. We didn't learn all of the header files and my professsor wouldn't allow us to use different header files that we didnt learn.

    Theres not operator I can use like the + to concatenate 2 different data types?

  4. #4
    Lively Member
    Join Date
    Jan 2004
    Posts
    82

    Re: integer and string concatenation

    Quote Originally Posted by SomethinCool
    Theres not operator I can use like the + to concatenate 2 different data types?
    I'm afraid not...

    Are you allowed to use <stdio.h> for sprintf? (But this is C I believe...)
    You also have ltoa() ...
    Last edited by NickMeuir; May 1st, 2005 at 10:04 PM.

  5. #5
    Fanatic Member
    Join Date
    Jan 2005
    Location
    Cebu
    Posts
    607

    Re: integer and string concatenation


  6. #6

    Thread Starter
    Frenzied Member SomethinCool's Avatar
    Join Date
    Jan 2001
    Location
    Malvern, PA
    Posts
    1,407

    Re: integer and string concatenation

    Thanks for the quick responses.

    I don't think I'll be able to use sstream. Since we haven't learned it, I think I will get points off for using it. He said people who are more experienced can't use things that we haven't learned.

    This is what i'm trying to do...read assignment #4:
    Click Here

    I'm trying to add each order into a string array so when the user asks for the order number it will just call the position in the array and output what was in the array.

  7. #7

    Thread Starter
    Frenzied Member SomethinCool's Avatar
    Join Date
    Jan 2001
    Location
    Malvern, PA
    Posts
    1,407

    Re: integer and string concatenation

    I actually figured out a different way to do it. Thanks for the help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width