String Concatetation
I have variables a, b, c = 1, 2, 3.
I would like to make it a string abc.
int a = 1;
int b = 2;
int c = 3;
string abc = "";
abc = a + "" + b + "" + c ;
count << abc;
That above code does not work :(
Thanks, Regards X
OmniX
Practically a Master Poster
656 posts since Dec 2007
Reputation Points: 31
Solved Threads: 10
Use std::ostringstream class (from header) and its member function str().
It's so simple ;)..
ArkM
Postaholic
2,001 posts since Jul 2008
Reputation Points: 1,234
Solved Threads: 348
To be a bit clearer, you can use the stringstream class like so:
#include <iostream>
#include <sstream>
using namespace std;
int main() {
stringstream ss; // our stringstream object
int a = 1, b = 2, c = 3;
ss << a << b << c; // we insert a b and c into the stringstream
cout << ss.str(); // call str() to get the string out
system("pause");
return 0;
}
For more info on stringstream check out: http://www.cplusplus.com/reference/iostream/stringstream/
mahlerfive
Junior Poster in Training
77 posts since Aug 2008
Reputation Points: 33
Solved Threads: 18
mahlerfive thankyou very much for your help, much appericated.
Regards X
OmniX
Practically a Master Poster
656 posts since Dec 2007
Reputation Points: 31
Solved Threads: 10
This isn't very related to the question, but mahlerfive, here is a few reasons why not to use system("pause") .
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
On the topic of pause, you got one for cls?
Thanks, Regards X
OmniX
Practically a Master Poster
656 posts since Dec 2007
Reputation Points: 31
Solved Threads: 10
Nope sorry, but its possible to clear the console buffer without using a system call. You will just have to look it up.
William Hemsworth
Posting Virtuoso
1,591 posts since Mar 2008
Reputation Points: 1,429
Solved Threads: 129
On the topic of pause, you got one for cls?
Here are a few options. I prefer option 4 myself :)
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
Hahahaha
I just pissed myself, im expecting serious input at option 4 and boom ahhh a good laugh.
But on a serious note, I liked option 1 - clear and simple.
Agree, disagree?
On a side note:
for
system("pause")
we should use cin.get()?
Or something like make a function an int and return 0, something like that but cins the way to go i gather
ahh good laugh off to bed goodnight people
OmniX
Practically a Master Poster
656 posts since Dec 2007
Reputation Points: 31
Solved Threads: 10
I like option 1 in all it's simplicity, but it's main disadvantage is that the cursor is at the bottom of the screen after using this method. So the next time you write anything to the screen, it will be written at the bottom rather then the top.
I personally like (and use) the snippet from option 6
system("pause")
we should use cin.get()?
sort of. Here's another link for you :)
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
I just read the whole 'thread' abit over whelming but I gathered:
cin.get(); // when no output after
//when output is after
#include <istream>
void ignore_line ( std::istream& in )
{
char ch;
while ( in.get ( ch ) && ch != '\n' )
;
}
Thanks for the help and info once again niek_e!
OmniX
Practically a Master Poster
656 posts since Dec 2007
Reputation Points: 31
Solved Threads: 10