473,385 Members | 2,013 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,385 software developers and data experts.

format string

in the program im writing, i was going to output this using cout :
cout << left << setw(10) << newAccount.getNumber() << setw(18)
<< newAccount.getName() << setw(18) << newAccount.getFamName() <<
setw(11)
<< setprecision(2) << right << fixed << showpoint<<
newAccount.getBalance() << 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.getNumber() << setw(18)
<< newAccount.getName() << setw(18) << newAccount.getFamName() <<
setw(11)
<< setprecision(2) << right << fixed << showpoint<<
newAccount.getBalance() << endl;
Jul 22 '05 #1
3 3721
"Someonekicked" <so***********@comcast.net> wrote...
in the program im writing, i was going to output this using cout :
cout << left << setw(10) << newAccount.getNumber() << setw(18)
<< newAccount.getName() << setw(18) << newAccount.getFamName() <<
setw(11)
<< setprecision(2) << right << fixed << showpoint<<
newAccount.getBalance() << 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.getNumber() << setw(18)
<< newAccount.getName() << setw(18) << newAccount.getFamName() <<
setw(11)
<< setprecision(2) << right << fixed << showpoint<<
newAccount.getBalance() << endl;


Read about, and use, 'ostringstream'.

V
Jul 22 '05 #2

"Someonekicked" <so***********@comcast.net> wrote in message
news:N5********************@comcast.com...
in the program im writing, i was going to output this using cout :
cout << left << setw(10) << newAccount.getNumber() << setw(18)
<< newAccount.getName() << setw(18) << newAccount.getFamName() <<
setw(11)
<< setprecision(2) << right << fixed << showpoint<<
newAccount.getBalance() << 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.getNumber() << setw(18)
<< newAccount.getName() << setw(18) << newAccount.getFamName() <<
setw(11)
<< setprecision(2) << right << fixed << showpoint<<
newAccount.getBalance() << 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::ostringstream 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
"Someonekicked" <so***********@comcast.net> wrote in message
news:N5********************@comcast.com...
in the program im writing, i was going to output this using cout :
cout << left << setw(10) << newAccount.getNumber() << setw(18)
<< newAccount.getName() << setw(18) << newAccount.getFamName() <<
setw(11)
<< setprecision(2) << right << fixed << showpoint<<
newAccount.getBalance() << 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.getNumber() << setw(18)
<< newAccount.getName() << setw(18) << newAccount.getFamName() <<
setw(11)
<< setprecision(2) << right << fixed << showpoint<<
newAccount.getBalance() << endl;

Jul 22 '05 #4

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

Similar topics

2
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...
6
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...
11
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 <=...
2
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...
7
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...
4
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...
6
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...
8
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...
7
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
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...

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.