Solved

String formatting in C++

Posted on 2006-11-21
2
185 Views
Last Modified: 2010-04-02
Hi Experts,

If I want to print something formatted in certain way in C,

printf("%s - %d - %s\n", str1, num, str2);


Is there C++ functions similar to printf for formatted output.  If so, how do I use it?
0
Comment
Question by:ambuli
  • 2
2 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 17988543
Just use 'cout' like

cout << str1 << " - " << num << " - " << str2 << endl;
0
 
LVL 86

Accepted Solution

by:
jkr earned 300 total points
ID: 17988557
BTW, the equivalent or 'sprintf()' would be a stringstream, e.g. instead of

sprintf(buf,"%s - %d - %s\n", str1, num, str2);
printf("%s",buf);

you'd use

#include <sstream>
using namespace std;

stringstream ss;

ss << str1 << " - " << num << " - " << str2 << endl;
cout << ss.str();
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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…
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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

708 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

16 Experts available now in Live!

Get 1:1 Help Now