| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Someonekicked" |
| Date: |
11 Dec 2004 01:34:31 PM |
| Object: |
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;
.
|
|
| User: "Mike Wahler" |
|
| Title: Re: format string |
11 Dec 2004 02:19:57 PM |
|
|
"Someonekicked" <someonekicked@comcast.net> wrote in message
news:N5Cdna4YEeySzSbcRVn-tA@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
.
|
|
|
|
| User: "Victor Bazarov" |
|
| Title: Re: format string |
11 Dec 2004 02:02:21 PM |
|
|
"Someonekicked" <someonekicked@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
.
|
|
|
|
| User: "Someonekicked" |
|
| Title: Re: format string |
11 Dec 2004 02:23:21 PM |
|
|
thx for ur replies, that was very helpful
"Someonekicked" <someonekicked@comcast.net> wrote in message
news:N5Cdna4YEeySzSbcRVn-tA@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;
.
|
|
|
|

|
Related Articles |
|
|