473,769 Members | 6,799 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

format string

in the program im writing, i was going to output this using cout :
cout << left << setw(10) << newAccount.getN umber() << setw(18)
<< newAccount.getN ame() << setw(18) << newAccount.getF amName() <<
setw(11)
<< setprecision(2) << right << fixed << showpoint<<
newAccount.getB alance() << endl;
my question is, i want to declare a string , say result, and let result be
equal to the above output;
is there a way to format a string using left, setw or similar functions?

i know the following wont work, but (thats actually what i need to do ) :

string result;
result = left << setw(10) << newAccount.getN umber() << setw(18)
<< newAccount.getN ame() << setw(18) << newAccount.getF amName() <<
setw(11)
<< setprecision(2) << right << fixed << showpoint<<
newAccount.getB alance() << endl;
Jul 22 '05 #1
3 3739
"Someonekic ked" <so***********@ comcast.net> wrote...
in the program im writing, i was going to output this using cout :
cout << left << setw(10) << newAccount.getN umber() << setw(18)
<< newAccount.getN ame() << setw(18) << newAccount.getF amName() <<
setw(11)
<< setprecision(2) << right << fixed << showpoint<<
newAccount.getB alance() << endl;
my question is, i want to declare a string , say result, and let result be
equal to the above output;
is there a way to format a string using left, setw or similar functions?

i know the following wont work, but (thats actually what i need to do ) :

string result;
result = left << setw(10) << newAccount.getN umber() << setw(18)
<< newAccount.getN ame() << setw(18) << newAccount.getF amName() <<
setw(11)
<< setprecision(2) << right << fixed << showpoint<<
newAccount.getB alance() << endl;


Read about, and use, 'ostringstream' .

V
Jul 22 '05 #2

"Someonekic ked" <so***********@ comcast.net> wrote in message
news:N5******** ************@co mcast.com...
in the program im writing, i was going to output this using cout :
cout << left << setw(10) << newAccount.getN umber() << setw(18)
<< newAccount.getN ame() << setw(18) << newAccount.getF amName() <<
setw(11)
<< setprecision(2) << right << fixed << showpoint<<
newAccount.getB alance() << endl;
my question is, i want to declare a string , say result, and let result be
equal to the above output;
is there a way to format a string using left, setw or similar functions?

i know the following wont work, but (thats actually what i need to do ) :

string result;
result = left << setw(10) << newAccount.getN umber() << setw(18)
<< newAccount.getN ame() << setw(18) << newAccount.getF amName() <<
setw(11)
<< setprecision(2) << right << fixed << showpoint<<
newAccount.getB alance() << endl;


Create an 'ostringstream' object (this type is declared by the
standard header <sstream>). Apply the same operations to the
ostringstream object that you would have to e.g. 'cout'. When done,
the ostringstream's 'str()' member function will (barring any
errors writing to the ostringstream) return a 'std::string'
(this type is declared by standard header <string>) containing the
formatted data.

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

int main()
{
std::ostringstr eam oss;

for (int i = 7; i < 15; ++i)
oss << std::setw(3) << i << '\n';

std::string output(oss.str( ));
std::cout << output << '\n';

return 0;
}
-Mike
Jul 22 '05 #3
thx for ur replies, that was very helpful
"Someonekic ked" <so***********@ comcast.net> wrote in message
news:N5******** ************@co mcast.com...
in the program im writing, i was going to output this using cout :
cout << left << setw(10) << newAccount.getN umber() << setw(18)
<< newAccount.getN ame() << setw(18) << newAccount.getF amName() <<
setw(11)
<< setprecision(2) << right << fixed << showpoint<<
newAccount.getB alance() << endl;
my question is, i want to declare a string , say result, and let result be
equal to the above output;
is there a way to format a string using left, setw or similar functions?

i know the following wont work, but (thats actually what i need to do ) :

string result;
result = left << setw(10) << newAccount.getN umber() << setw(18)
<< newAccount.getN ame() << setw(18) << newAccount.getF amName() <<
setw(11)
<< setprecision(2) << right << fixed << showpoint<<
newAccount.getB alance() << endl;

Jul 22 '05 #4

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

Similar topics

2
4044
by: san | last post by:
Hello, all! I have question about String.Format method. There are two variants: public static string Format(string, params object); and public static string Format(IFormatProvider, string, params object); What happens if i will use String.Format("{0} and {1}", "one", "two") instead of String.Format(System.Globalization.CultureInfo.CurrentCulture, "{0} and {1}", "one", "two")?
6
2835
by: Stuart McGraw | last post by:
I am looking for a VBA "format" or "template" function, that is, a function that takes a format string and a varying number of arguments, and substitutes the argument values into the format string as specified in the format string. For example fmt("this is $1 format string. arg2 is $2", "short", 3) would return the string "this is short format string. arg2 is 3" Now, the above is easy to write and I have done so. What I want is...
11
5952
by: Grumble | last post by:
Hello, I have the following structure: struct foo { char *format; /* format string to be used with printf() */ int nparm; /* number of %d specifiers in the format string */ /* 0 <= nparm <= 4 */ };
2
36868
by: Bob | last post by:
I'm having trouble the string.Format() throwing exceptions and I can't figure out what I am doing wrong. Given the following setup code: string str = { "one", "two", "three", "four" }; double val = { 1.0, 2.0, 3.0, 4.0 }; string fmt = "{0} {1} {2} {3}"; The following variations of string.Format() will work just fine with strings: string s1 = String.Format(fmt, str, str, str, str); // works
7
6496
by: Alpha | last post by:
Hi, I'm maintaining C# code and am fairly new with C# programming. I'm looking for codes that's droping the 2nd digit of a nuber printed out and I suspect it's the code below. Can someone tell me where I can look up explaination on the String.Format, the format string part like "("{0:.#0}". What's the trailing 0 and what is that "0:" means? I assume the "#" is the place holder character for th value obtain after the comma in the code. ...
4
1638
by: David Morris | last post by:
Hi Could somebody please explain what the following line of code means String.Format("{0}\{1}.{2:00}", C:\, myfile.txt, 1 It's actually the first argument that I don't understand. What is the purpose of "{0}\{1}.{2:00}"? I've been using VB .NET for a year now, and have never come across anything like this Best Regards David Morris
6
5237
by: Scewbedew | last post by:
Suppose I have the following code: string myFormat = "Line1/nLine 2"; string formattedString = string.Format(myFormat); ....that would produce a 2-line output as expected. But if I load that very same format string from an xml file: ....load xmlNode WorkNode...
8
4992
by: Lucky | last post by:
hi guys! back again with another query. the problem is like this. i want to print a line like this: "---------------------------------------------" the easiest way is to simply assign it to string and print it. but i want to use the String.Format() method if possible to do it.
7
3073
by: Rick | last post by:
With String.Format, if I have an incorrect number of args specified for a format string, compile fails. How can I implement similar design-time functionality for my own string functions?
8
2223
by: Armando Rocha | last post by:
Hi, Hi have a string with 16 chars "25DD68EDEB8D5E11" and i want show it in form like this "25DD-68ED-EB8D-5E11", i try String.Format("{0:####-####-####-####}", mystr), but not work, i think that it is because the "mystr" it is already a string. Anyone can help me? Sorry my english....
0
9424
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10223
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10051
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10000
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9866
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3968
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3571
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.