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

Recommended Answers

All 10 Replies

Use std::ostringstream class (from <sstream> header) and its member function str().
It's so simple ;)..

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/

commented: Perfect example, thankyou. +1

mahlerfive thankyou very much for your help, much appericated.

Regards X

This isn't very related to the question, but mahlerfive, here is a few reasons why not to use system("pause") .

On the topic of pause, you got one for cls?

Thanks, Regards X

Nope sorry, but its possible to clear the console buffer without using a system call. You will just have to look it up.

On the topic of pause, you got one for cls?

Here are a few options. I prefer option 4 myself :)

commented: Nice informative post :) +1

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

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 :)

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.