在我的getline()读入字符串后,我无法读取整数。我必须从数据文件中读取整个名称,然后读入支付率,然后是家属的数量,然后是总的百分比。我可以使用Arrays循环向量或任何类似的东西。我只能使用getline()和Ignore()函数或类似的函数。所以我的问题是我在哪里错了?
以下是数据文件的样子(我只编写了一个人阅读的内容):
John W. Smith
12.55
3
5
玛丽安德森11.75
1
8
Brad W. Baker
11.75
0
0
希瑟约翰逊13.25
2
10
到目前为止,这是我的代码:
#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;
}
答案 0 :(得分:0)
将retirement = percgross * gross;
更改为retirement = percgross * gross / 100;
您读取了毛的百分比,并将其直接乘以,然后retirement
大于gross
。
修改强>
对于多人场景,请使用循环语句。
#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;
}