format string



 DEVELOP > c-Plus-Plus > format string

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
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;

.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER