How to display two decimals places in the final result

This is my first time posting on this website so bare with me

How do I make the final result display two decimal places? (since I am working with money.

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;


int main()
{
setprecision(2);
system("color f0");
ifstream inputfile;

//Declare variables
string Fname = " ";
string Lname = " ";
string ssn = " ";
int count = 0;
int hours = 0;
double overtime = 0.00;
double rate = 0.00;
double gross = 0.00;
double net = 0.00;
double deduct = 0.00;
inputfile.open("Payroll.txt");
inputfile >> Fname >> Lname >> ssn >> hours >> rate;


cout << "\t\t************************************" << endl;
cout << "\t\t* *" << endl;
cout << "\t\t* *" << endl;
cout << "\t\t* *" << endl;
cout << "\t\t************************************" << endl;

//Header
cout << "SSN \tName \tHours \tRate \tGross \tDeductions \tNetPay" << endl;
cout << "___ \t____ \t____\t____ \t_____ \t____ \t\t______" << endl;

//Checking File
while (inputfile.eof() == false)
{
count++;

//Math
if (hours > 40)
{
overtime = (40 * rate) + (hours - 40) * rate * 1.5;
gross = overtime;
deduct = gross * 0.1;
net = gross - deduct;

}
else

gross = hours * rate;
deduct = gross * 0.1;
net = gross - deduct;

//edit name



//display

cout << " " << ssn.substr(7) << " \t" << " " << Fname << "\t" << " " << hours << "\t" << " "
<< rate << "\t" << " " << gross << "\t" << " " << deduct << "\t\t" << " " << net << endl;
inputfile >> Fname >> Lname >> ssn >> hours >> rate;

}
cout << endl;
cout << "Numbers of Records: " << count << endl;
cout << "\n T H A N K Y O U\n" << endl;
system("pause");
return 0;
}
Last edited on
The compiler isn't reading from a file, your program is.*

while (inputfile.eof() == false)
In general, don't loop on eof for this purpose. It doesn't do what you want, because the eof flag is only set after failure to get input.

But more importantly, you don't ever read anything from your file inside the loop, so the loop will never terminate.

Loop like this instead (assuming your file is structured in a sane way):
1
2
3
4
while (inputfile >> Fname >> Lname >> ssn >> hourly >> rate)
{
    // ...
}


*inb4 someone tells me that that's exactly what a compiler does.
Last edited on
Topic archived. No new replies allowed.