I cant read in the integers after my getline() reads in the string. I have to read in the whole entire name from a data file, then read in the pay rate, then the number of dependants, and then the percentage of gross. I can use Arrays loops vectors or anything of that sort. I can only use the getline() and Ignore() functions or something similar to that. So my question is where am I going wrong here?
Here is what the data file looks like (I only coded for it to read in one person):
John W. Smith
12.55
3
5
Mary Anderson
11.75
1
8
Brad W. Baker
11.75
0
0
Heather Johnson
13.25
2
10
Here is my code so far:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ifstream indata;
ofstream outdata;
string fname, lname;
int hours;
hours = 0;
outdata.open("Weeklypay.dat");
double payrate;
double gross;
double taxes;
double ssecurity;
int dependants;
double retirement;
double net;
double percgross;
int insurance;
indata.open("Pay.dat");
getline(indata, fname);
cout << "Please enter the total hours worked for " << fname << endl;
cin >> hours;
indata >> payrate >> insurance;
gross = payrate * hours;
taxes = 0.23 * gross;
ssecurity = 0.08 * gross;
dependants = 12 * insurance;
indata >> percgross;
retirement = percgross * gross / 100;
net = gross - taxes - ssecurity - dependants - retirement;
cout << fname << "'s net pay is: $" << net << endl;
outdata << fname << endl;
outdata << "Gross Pay: $" << gross << endl;
outdata << "Taxes: $" << taxes << endl;
outdata << "Social Security: $" << ssecurity << endl;
outdata << "Insurance: $" << dependants << endl;
outdata << "Retirement: $" << retirement << endl;
outdata << endl << endl;
outdata << "Net Pay: $" << net << endl;
outdata << endl << endl;
// Next person here:
cin.ignore(10, '\n');
getline(indata, fname);
cout << "Please enter the total hours worked for " << fname << lname << endl;
cin >> hours;
indata >> payrate;
gross = payrate * hours;
taxes = 0.23 * gross;
ssecurity = 0.08 * gross;
indata >> insurance;
dependants = 12 * insurance;
indata >> percgross;
retirement = percgross * gross / 100;
net = gross - taxes - ssecurity - dependants - retirement;
cout << fname << lname << "'s net pay is: $" << net << endl;
outdata << fname << lname << endl;
outdata << "Gross Pay: $" << gross << endl;
outdata << "Taxes: $" << taxes << endl;
outdata << "Social Security: $" << ssecurity << endl;
outdata << "Insurance: $" << dependants << endl;
outdata << "Retirement: $" << retirement << endl;
outdata << endl << endl;
outdata << "Net Pay: $" << net << endl;
outdata << endl << endl;
indata.close();
outdata.close();
return 0;
}
change retirement = percgross * gross;
to retirement = percgross * gross / 100;
You read in the percentage of gross, and multiply it directly, then the retirement
is bigger than gross
.
EDIT:
for the multiple persons scenario, use a loop statement.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ifstream indata;
ofstream outdata;
string fname, lname;
int hours;
hours = 0;
outdata.open("Weeklypay.dat");
double payrate;
double gross;
double taxes;
double ssecurity;
int dependants;
double retirement;
double net;
double percgross;
int insurance;
indata.open("Pay.dat");
while(getline(indata, fname) )
{
if(fname.empty() )
continue;
cout << "Please enter the total hours worked for " << fname << endl;
cin >> hours;
indata >> payrate >> insurance;
gross = payrate * hours; //690.25
taxes = 0.23 * gross;//158.75
ssecurity = 0.08 * gross;//51.7
dependants = 12 * insurance;//3
indata >> percgross;
retirement = percgross * gross / 100;//34.51
net = gross - taxes - ssecurity - dependants - retirement;
cout << fname << "'s net pay is: $" << net << endl;
outdata << fname << endl;
outdata << "Gross Pay: $" << gross << endl;
outdata << "Taxes: $" << taxes << endl;
outdata << "Social Security: $" << ssecurity << endl;
outdata << "Insurance: $" << dependants << endl;
outdata << "Retirement: $" << retirement << endl;
outdata << endl << endl;
outdata << "Net Pay: $" << net << endl;
outdata << endl << endl;
}
indata.close();
outdata.close();
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.