Solved

String formatting in C++

Posted on 2006-11-21
2
187 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

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…
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 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…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

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