Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

String formatting in C++

Posted on 2006-11-21
2
Medium Priority
?
196 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
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 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.

604 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