Displaying ".00"
acevans2 (27)
Sep 19, 2008 at 3:02pm UTC
Hello guys. I just had a quick question.
I've got a program that is supposed to display information about net pay, social security withheld, etc. and thus needs to show dollar amounts like "1.00" instead of just "1"
How do I do that? I've got it all working otherwise. Everything is declared as floats and all that.
Thanks
acevans2 (27)
Sep 19, 2008 at 3:11pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
#include <iostream>
#include <iomanip>
using namespace std;
float sscons = .08;
float withcons = .10;
int main()
{
float hrs;
float rate;
float gross;
float netpay;
float sstax, withtax;
float grosstotal = 0;
float sstotal = 0;
float checktotal = 0;
float withtotal = 0;
float avggross;
cout << "Enter hours worked (0 to quit): " ;
cin >> hrs;
while (hrs > 0)
{
if (hrs > 40)
{
cout << "Enter pay rate: " ;
cin >> rate;
cout << endl;
gross = ((hrs-40) * rate * 1.5) + (40 * rate);
sstax = gross * sscons;
withtax = gross * withcons;
netpay = gross - sstax - withtax;
cout << setprecision (4) << "Gross pay: " << gross << endl << "SS Withheld: "
<< sstax << endl << "Withholding Tax: " << withtax << endl << "Net Pay: " << netpay << endl;
checktotal+=1;
grosstotal+=gross;
sstotal+=sstax;
withtotal+=withtax;
cout << endl << "Enter hours worked (0 to quit): " ;
cin >> hrs;
}
else
{
cout << "Enter pay rate: " ;
cin >> rate;
cout << endl;
gross = hrs * rate;
sstax = gross * sscons;
withtax = gross * withcons;
netpay = gross - sstax - withtax;
cout << setprecision (4) << "Gross pay: " << gross << endl << "SS Withheld: "
<< sstax << endl << "Withholding Tax: " << withtax << endl << "Net Pay: " << netpay << endl;
checktotal+=1;
grosstotal+=gross;
sstotal+=sstax;
withtotal+=withtax;
cout << endl << "Enter hours worked (0 to quit): " ;
cin >> hrs;
}
}
avggross = grosstotal / checktotal;
cout << endl << "TOTALS" << endl << endl << "Total Payroll: " << grosstotal << endl << "Number of Checks: " << checktotal << endl
<< "Average Paycheck: " << avggross << endl << endl << "Total SS withheld: " << sstotal << endl << "Total Withholding: "
<< withtotal << endl;
return 0;
}
I put it in where that example made it look like it should go, but it didn't work.
I still see whole numbers without following decimals like .00.
Do you see what I did wrong?
Duoas (6752)
Sep 19, 2008 at 3:41pm UTC
You also need to use the fixed flag.
cout << fixed << setprecision( 2 ) << 3.14159 << endl;
Topic archived. No new replies allowed.