Solved

Multiple String Concantenation in C++

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now