?
Solved

String formatting in C++

Posted on 2006-11-21
2
Medium Priority
?
195 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
[X]
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
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 1200 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

777 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