473,404 Members | 2,195 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

printing to a string class variable

hey all,

can anyone enlighten me on the proper c++ way to print formatted text into a
string?

e.g.

string mytext;
double nn=445566.332211;

what is the proper equivalent of the following c type expression:

sprintf(mytext,"%8.3f",nn); // this is wrong in c++!!

and then to add to it?

mytext += more formatted text

any ideas?

thnx a bunch:)

lou

Jul 22 '05 #1
4 2199
The most appropriate way to format into a string value is to use
std::ostringstream declared in <sstream>.

Regards,

Jon Trauntvein

Jul 22 '05 #2
In article <20***************************@mb-m10.aol.com>,
Illuzioner <il********@aol.com> wrote:

string mytext;
double nn=445566.332211;

what is the proper equivalent of the following c type expression:

sprintf(mytext,"%8.3f",nn); // this is wrong in c++!!

and then to add to it?

mytext += more formatted text


The key concept is "stringstream".

#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>

using namespace std;

int main ()
{
string mytext;
ostringstream mytextstream;
double nn = 445566.332211;

// do output to a stringstream just like to cout or to a file

mytextstream << fixed << showpoint;
mytextstream << setw(8) << setprecision(3) << nn;
mytextstream << " is the number.";

// extract the contents of the stringstream to a string

mytext = mytextstream.str();
mytext += " Is this OK?\n";

cout << mytext;

return 0;;
}
--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #4

"Illuzioner" <il********@aol.com> wrote in message
news:20***************************@mb-m10.aol.com...
hey all,

can anyone enlighten me on the proper c++ way to print formatted text into a string?
There are a couple of ways: Using a 'std::ostringstream'
object, or use 'sprintf()'.

e.g.

string mytext;
double nn=445566.332211;

what is the proper equivalent of the following c type expression:

sprintf(mytext,"%8.3f",nn); // this is wrong in c++!!
It's wrong because you gave the wrong type argument to
'sprintf()'. So it's wrong in C as well.

#include <ios>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>

int main()
{
double nn=445566.332211;
std::ostringstream oss;
oss << std::fixed << std::setprecision(6) << nn;
std::string mytext(oss.str());
std::cout << mytext << '\n';
return 0;
}

and then to add to it?

mytext += more formatted text
oss.str("");
oss << " more text " << 42;
mytext += oss.str();


any ideas?


Yes, get this book:
www.josuttis.com/libbook

-Mike
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Thomas Philips | last post by:
I want to print "1 spam 4 you" using a formatted string that gets its inputs from the dictionary d={'n1':1, 's1':'spam', 'n2':4}. To do so, I write >>> x="%('n1')d %('s1')s %('n2')d you" >>> x...
2
by: Jody Burgess | last post by:
Hi; I am writing my first python program and would like to know how to change stdout to refer to my default printer or any other printer on my network. The other question is, Is there an API set...
1
by: NickB | last post by:
Please could someone tell me what is wrong. Ther error is: An unhandled exception of type 'System.NullReferenceException' occurred in microsoft.visualbasic.dll Additional information: Object...
7
by: Steve M | last post by:
Hello, I'm having problems sending information from a python script to a printer. I was wondering if someone might send me in the right direction. I wasn't able to find much by Google TIA...
4
by: Rob T | last post by:
I have a small VB program that has a printing module...very simple....and works great. However, If I try to print to a generic printer, I get the following error: "The data area passed to a...
1
by: hamil | last post by:
I am trying to print a graphic file (tif) and also use the PrintPreview control, the PageSetup control, and the Print dialog control. The code attached is a concatination of two examples taken out...
3
by: islay | last post by:
Hello, I'm trying to print more than one page using the PrintDocument object. I set "e.HasMorePages = True" after completeing the first page in the "PrintDocument1_PrintPage" subroutine, and...
4
by: Lucas Ponzo | last post by:
Hi All, I have an ASP.NET 2.0 app. The users access the pages, uniquely via pocket pc ... I need to print a page. But I need that the page print on a printer installed on the web server...
1
by: Glenn | last post by:
I am writing a program for field work that will use a receipt printer. I need to be able to adjust the page settings prior to printing depending on how much needs to be printed. I have been able...
0
it0ny
by: it0ny | last post by:
Hi guys, thanks I am fairly new to this forum so I hope I chose the right place to post this question. I try to make my program printout a deposit's report. I created a class to store the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.