473,404 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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
2 3239

<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Matthew David Hills | last post by:
If setw() is being used to limit how many characters are pulled from a stream, should the # of characters taken from the stream depend on whether the stream is being sent to a std::string or a...
5
by: Mastupristi | last post by:
I want to obtain the c++ equivalent of: unsigned short us = 347; printf("0x%04hX",us); that outputs "0x015B" I ried with: cout.setf(ios_base::hex,ios_base::basefield);
4
by: Mastupristi | last post by:
I have an array of char for example: char tag = {1, 'W', 0xFF, 0xFF}; I want to obtain: 01 57 FF FF if I try with:
1
by: | last post by:
The code: // Block 1 // cout << "Integrating " << str << " on " << endl << " exact answer: " << exact << endl << flush; #if 0 // Block 2
9
by: liaoo | last post by:
Dear all, I have a question about using cout<<... In standard C, I can use printf("%x",...) to print "hex" number. But in Watcom C++, when using cout<<, I got the "decimal" representation ! ...
4
by: alternativa | last post by:
Hi, I'd like to obtain an output looking as follows: name number phone address Caroline 233 34234 White St. 12 Anna 929043 093284 Brown St. 325...
8
by: Martin Jørgensen | last post by:
Hi, I have a program that reads in a couple of numbers from files and then stores them with some latex-code but I'm not satisfied with the exponential output, as it takes up too many character...
3
by: shk253 | last post by:
Hi all - I'm trying to generate a payroll. I'm having trouble with the cout statements overwriting the previous inputs, e.g. enter info for employe 1: blah blah blah. Enter info for employee 2: blah...
4
by: mar11 | last post by:
Hi all, i have the following example for manipulation the output cout <<"*** the value"<< setw (10)<<"okay"<<endl; cout <<"******* the value"<< setw (10)<<"okay"<<endl; and i get this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.