Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Multiple String Concantenation in C++

Posted on 2007-11-15
Medium Priority
Last Modified: 2009-12-16
What I am  trying to do is concatenate multiple strings together and store it in another string variable to use later. Below is my latest attempt at my problem.   I am new to C++ strings, so any help is appreciated.  Thanks for your time.

string output << "Printing '" << fieldText[9] << "' using '" << printerName << "' on " << someDate << endl;
cout << output << endl;

Open in new window

Question by:dleehanson
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Assisted Solution

UrosVidojevic earned 80 total points
ID: 20290739

Accepted Solution

Ichijo earned 120 total points
ID: 20291086
Change 'string' to 'ostringstream'.

Then if you later need to access it as a string (for example, to assign it to a string variable), call output.str().
ostringstream output;
output << "Printing '" << fieldText[9] << "' using '" << printerName << "' on " << someDate << endl;
cout << output << endl;

Open in new window


Author Comment

ID: 20291139
Thanks for the bump in the right direction.  I ended up using an ostringstream to get what I wanted.  Thanks again.

ostringstream os;
os << "Printing '" << fieldText[9] << "' using '" << printerName << "' on " << someDate << endl;
std::string output(os.str());
cout << output;

Open in new window


Author Comment

ID: 20291163
Heh, that's what I get for not refreshing the page...

So I don't need to explicitly convert it to a string first then?  Awesome.  Thanks for the help you two.


Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
Suggested Courses

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question