format string

S

Someonekicked

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;
 
V

Victor Bazarov

Someonekicked said:
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
 
M

Mike Wahler

Someonekicked said:
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::eek:stringstream 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,184
Messages
2,570,979
Members
47,579
Latest member
CharaS3188

Latest Threads

Top