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
Solved

Multiple String Concantenation in C++

Posted on 2007-11-15
4
1,028 Views
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.

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

Open in new window

0
Comment
Question by:dleehanson
  • 2
4 Comments
 
LVL 7

Assisted Solution

by:UrosVidojevic
UrosVidojevic earned 20 total points
ID: 20290739
0
 
LVL 4

Accepted Solution

by:
Ichijo earned 30 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

0
 
LVL 1

Author Comment

by:dleehanson
ID: 20291139
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
 
LVL 1

Author Comment

by:dleehanson
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.

--Charly
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

860 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