467,867 Members | 1,271 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,867 developers. It's quick & easy.

cout with setw ?

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
  • viewed: 3047
Share:
2 Replies

<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
Thanks Mike.

Appreciated for your time !

Nov 2 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Matthew David Hills | last post: by
5 posts views Thread by Mastupristi | last post: by
4 posts views Thread by Mastupristi | last post: by
9 posts views Thread by liaoo | last post: by
4 posts views Thread by alternativa | last post: by
8 posts views Thread by Martin Jørgensen | last post: by
4 posts views Thread by mar11 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.