Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1033
  • Last Modified:

Multiple String Concantenation in C++

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.

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

Open in new window

0
dleehanson
Asked:
dleehanson
  • 2
2 Solutions
 
UrosVidojevicCommented:
0
 
IchijoCommented:
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

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

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

Open in new window

0
 
dleehansonAuthor Commented:
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.

--Charly
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now