简体   繁体   English

使用getline()和Ignore()函数时,Cant似乎无法让我的ifstream和ofstream正常工作。Cant Use循环或数组等

[英]Cant seem to get my ifstream and ofstream to work properley when using getline() and Ignore() functions Cant Use Loops or Arrays ect

I cant read in the integers after my getline() reads in the string. 我的getline()读取字符串后,我无法读取整数。 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. 我可以使用Arrays循环向量或类似的任何东西。 I can only use the getline() and Ignore() functions or something similar to that. 我只能使用getline()和Ignore()函数或类似的函数。 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 12.55

3 3

5 5

Mary Anderson 玛丽·安德森

11.75 11.75

1 1个

8 8

Brad W. Baker 布拉德·贝克

11.75 11.75

0 0

0 0

Heather Johnson 希瑟·约翰逊(Heather Johnson)

13.25 13.25

2 2

10 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; 变更retirement = percgross * gross; to retirement = percgross * gross / 100; retirement = percgross * gross / 100; You read in the percentage of gross, and multiply it directly, then the retirement is bigger than gross . 您读入毛额的百分比,然后直接乘以,那么retirement就比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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM