Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.4k views
in Technique[技术] by (71.8m points)

need help pulling multiple items from text file in c++

Here is the code. Ignore the commented out code. Basically I am trying to pull items from a text file and assign them to the hourlyEmp1 through hourlyEmp3 items.

This is what the txt file looks like:

Caleb
21
40 0 40 0 40 0 40 
John
16
40 0 40 0 40 0 40
Jason
15
40 0 40 0 40 0 40

I can get Calebs information assigned to hourlyEmp1MonthlyGrossPay, but then it stops at that.

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

using namespace std;

double getInfoOneEmp(ifstream &inFile, string& tempName, const int& WEEKS);
void employeeOutput(ofstream& outFile, const string& tempName, const double& tempGrossPay);


int main() {

    string hourlyEmp1 = " ", hourlyEmp2 = " ", hourlyEmp3 = " ";
    const int WEEKS = 4;
    double hourlyEmp1MonthlyGrossPay, hourlyEmp2MonthlyGrossPay, hourlyEmp3MonthlyGrossPay;
    double regHours, ovtHours, wageRate, totalPay;
    ifstream payrollInFile;
    ofstream payrollOutFile;
    int totalEmp = 4;

    payrollInFile.open("input.txt");
    payrollOutFile.open("output.txt");
    
    hourlyEmp1MonthlyGrossPay = getInfoOneEmp(payrollInFile, hourlyEmp1, WEEKS);
    payrollInFile.ignore(100, '
');
    employeeOutput(payrollOutFile, hourlyEmp1, hourlyEmp1MonthlyGrossPay);
    hourlyEmp2MonthlyGrossPay = getInfoOneEmp(payrollInFile, hourlyEmp2, WEEKS);
    payrollInFile.ignore(100, '
');
    employeeOutput(payrollOutFile, hourlyEmp2, hourlyEmp2MonthlyGrossPay);
    hourlyEmp3MonthlyGrossPay = getInfoOneEmp(payrollInFile, hourlyEmp3, WEEKS);
    payrollInFile.ignore(100, '
');
    employeeOutput(payrollOutFile, hourlyEmp3, hourlyEmp3MonthlyGrossPay);


    payrollInFile.close();
    payrollOutFile.close();



    /*for(int i = 0; i <= totalEmp; i++) {
        while (i <= totalEmp) {
            string answer;
            cout << "Would you like to enter tips for " <<hourlyEmp1<< " ?"<<endl;
            if(answer="y") {
                for(int x = 0; x <=WEEKS; x++) {
                    while(x <= WEEKS) {
                        cout<<"Please enter week: " << x + 1 << "tips:"<<endl;
                    }
                }
            }
        }
        
    }*/


}

double getInfoOneEmp(ifstream &inFile, string& tempName, const int& WEEKS)
{
    double regHours, ovtHours, wageRate, totalPay, MonthlyPay = 0;


    getline(inFile, tempName);
    inFile >> wageRate;

        for (int counter = 0; counter < WEEKS; counter++)
        {

            inFile >> regHours >> ovtHours;
        

            totalPay = (regHours * wageRate) + (ovtHours * wageRate * 1.5);
            MonthlyPay = MonthlyPay + totalPay;

    }

    return MonthlyPay;
}

/*void getEmployeeInfo(ifstream &inFile, string&tempName, double &tempWage) {

}*/

void employeeOutput(ofstream& outFile, const string& tempName, const double& tempGrossPay)
{
    outFile << fixed << showpoint << setprecision(2);
    outFile << "Total Pay for " << tempName << " for the month is $" << setfill('*') << setw(10) 
    << tempGrossPay << setfill(' ') << endl;

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...