In: Computer Science
Why my net pay is always 0, and how can I fix it ?
here's the code
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;
int main() {
ofstream out;
out.open("myData.txt");
string fname;
cout << "name?" << endl;
cin >> fname;
double salary;
cout << "salary?" << endl;
cin >> salary;
double fedTax = salary * 15 / 100;
double stateTax = salary* 3.5 / 100;
double SST = salary * 5.75 / 100;
double medicare = salary* 2.75 / 100;
double pension = salary * 5 / 100;
double insurance = 75.00;
double net = salary - fedTax - stateTax - SST - medicare - pension - insurance;
out << "Gross Salary " << salary << endl;
out << "Federal Tax " << fedTax << endl;
out << "State Tax " << stateTax << endl;
out << "Social Security " << SST << endl;
out << "Medicare Tax " << medicare << endl;
out << "Pension Plan " << pension << endl;
out << "Health Insurance " << insurance << endl;
out << "Net Pay = " << net << endl;
out.close();
cout << fixed << setprecision(2);
ifstream in;
string taxName;
string secondName;
double num;
in.open("myData.txt");
cout << fname << endl;
for (int i =1; i <=8; i ++) {
in >> taxName >> secondName >> num;
cout << setfill('.') << setw(10) << left << taxName
<< setfill('.') << setw(10) << left << secondName
<< setfill('.') << setw(10) << right << num << endl;
}
in.close();
cout << salary - fedTax - stateTax - SST - medicare - pension - insurance << endl;
return 0;
}
The reason you are getting net pay 0 is because you were writing the net pay into myData.txt in a wrong fashion.
The following line is used to write net pay:
out << "Net Pay = " << net << endl;
Hence, the net pay written into myData.txt would be like "Net Pay = 605.0"
Now, you are printing the data in the following fashion.
in >> taxName >> secondName >> num;
In the case of net pay, taxName would be "Net", secondName would be "Pay" and num would be "=" instead of 605.0. Since "=" is not a double, it prints 0.
The correct code snippet is given below.
CODE
///////////////////////////////////////////////////////////////
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;
int main() {
ofstream out;
out.open("myData.txt");
string fname;
cout << "name?" << endl;
cin >> fname;
double salary;
cout << "salary?" << endl;
cin >> salary;
double fedTax = salary * 15 / 100;
double stateTax = salary* 3.5 / 100;
double SST = salary * 5.75 / 100;
double medicare = salary* 2.75 / 100;
double pension = salary * 5 / 100;
double insurance = 75.00;
double net = salary - fedTax - stateTax - SST - medicare - pension - insurance;
out << "Gross Salary " << salary << endl;
out << "Federal Tax " << fedTax << endl;
out << "State Tax " << stateTax << endl;
out << "Social Security " << SST << endl;
out << "Medicare Tax " << medicare << endl;
out << "Pension Plan " << pension << endl;
out << "Health Insurance " << insurance << endl;
out << "Net Pay " << net << endl;
out.close();
cout << fixed << setprecision(2);
ifstream in;
string taxName;
string secondName;
double num;
in.open("myData.txt");
cout << fname << endl;
for (int i =1; i <=8; i ++) {
in >> taxName >> secondName >> num;
cout << setfill('.') << setw(10) << left << taxName
<< setfill('.') << setw(10) << left << secondName
<< setfill('.') << setw(10) << right << num << endl;
}
in.close();
cout << salary - fedTax - stateTax - SST - medicare - pension - insurance << endl;
return 0;
}
OUTPUT
///////////////////////////////////////////////////////////////