By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,515 Members | 1,772 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,515 IT Pros & Developers. It's quick & easy.

cout with setw ?

P: n/a
Below is my code:

#include <string>
#include <iostream>
#include <iomanip>
int main (void){
using namespace std;

string name1 = "JOHN";
int id1 = 1234;
float balance1 = 27000.00;
float available1 = 14000.00;
cout
<<left<<setw(20)<<"NAME"<<setw(20)<<"ID"<<setw(20) <<"BALANCE"<<setw(20)<<"AVAILABLE"<<endl;
cout
<<left<<setw(20)<<name1<<setw(20)<<id1<<setw(20)<< setprecision(15)<<balance1<<setw(20)<<available1<< endl;
}
output:

NAME ID BALANCE AVAILABLE
JOHN 1234 27000 14000

however, I would like to have the output as follow:

NAME ID BALANCE AVAILABLE
JOHN 1234 27000.00Cents 14000.00Cents

Thanks in advance for any hints.

Nov 2 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

<tv****@hotmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Below is my code:

#include <string>
#include <iostream>
#include <iomanip>
int main (void){
using namespace std;

string name1 = "JOHN";
int id1 = 1234;
float balance1 = 27000.00;
float available1 = 14000.00;
cout
<<left<<setw(20)<<"NAME"<<setw(20)<<"ID"<<setw(20) <<"BALANCE"<<setw(20)<<"AVAILABLE"<<endl;
cout
<<left<<setw(20)<<name1<<setw(20)<<id1<<setw(20)<< setprecision(15)<<balance1<<setw(20)<<available1<< endl;
}
output:

NAME ID BALANCE AVAILABLE
JOHN 1234 27000 14000

however, I would like to have the output as follow:

NAME ID BALANCE AVAILABLE
JOHN 1234 27000.00Cents 14000.00Cents

Thanks in advance for any hints.
stringstreams are useful for "pre-building" formatted strings:

#include <iomanip>
#include <ios>
#include <iostream>
#include <sstream>

int main()
{
double value(27000);

std::ostringstream oss;
oss << std::setprecision(2) << std::fixed;
oss << value << "Cents";

std::cout << std::left;
std::cout << std::setw(20) << "BALANCE" << '\n';
std::cout << std::setw(20) << oss.str() << '\n';
return 0;
}

Output:

BALANCE
27000.00Cents
-Mike

Nov 2 '05 #2

P: n/a
Thanks Mike.

Appreciated for your time !

Nov 2 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.