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.
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.
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?
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() ...
Re: integer and string concatenation
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.
Re: integer and string concatenation
I actually figured out a different way to do it. Thanks for the help.