C ++不将第一行数据插入输出文件

时间:2016-09-14 05:44:24

标签: c++

我对C ++比较陌生,并且一直在通过获取数据文件并读取数据文件,然后将数据与其中一些进行数学运算,然后将其全部导出到输出文件。但是我的问题是每次运行它都会跳过将第一行写入输出文件。

我的数据文件是:

Jon Doe 15 7.25
Nick Delgado 8 7.25
Jo Barnes 4 7.25
Harold Griffith 45 11.00
Linda Holmes 30 10.00

我的输出如下:

Nick Delgado 8 7.25 58 0 58
Jo Barnes 4 7.25 29 0 29
Harold Griffith 45 11 522.5 146.3 376.2
Linda Holmes 30 10 300 84 216

它完全不会写出任何有关Jon Doe的信息。这是:

Jon Doe 15 7.25 108.75 0 108.75

我尝试过多种不同的想法,问我的朋友们,总的来说,我非常沮丧。

我认为问题代码在这里:

    if (!dataOutFile.is_open()) {
        cout << "Couldn't open file";
    }
    else {
        dataOutFile << firstName << " " << lastName << " " << hoursWorked << " " << payRate << " " << grossPay << " " << withHolding << " " << takeHomePay << endl;
        cout << "What was wrote to file: \n" << firstName << " " << lastName << " " << hoursWorked << " " << payRate << " " << grossPay << " " << withHolding << " " << takeHomePay << endl;
    }

因为我的输出窗口如下所示:

enter image description here

所以它告诉我的是它正在转到写入文件的代码,因为它将第四个条目写入了第一个。但是根据输出窗口,它正在向文件写入它应该的信息 - 但由于某种原因它不是。我使用append命令所以它不应该覆盖任何东西,并且根据输出文件它不是,但可能是第一行。

没有错误或警告,我的调试日志没有错误。请帮助我,任何帮助表示赞赏。我也意识到代码有点混乱,我需要将其分解为更多功能,但我只是想让它工作,然后我可以清理它。

我的程序的完整代码处理所有这些,如果有人需要看到它。

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

// Global Variables
string fileName;
double totalGrossPay = 0;
double totalWithHolding = 0;
double totalTakeHomePay = 0;
double withHoldingLimit = 200;
double withHoldingRate = .28;
double overtimeRate = 1.5;

// Initialize Functions
void readFile();

int main() {
    cout << "What is the name of your file?" << endl;
    getline(cin, fileName);
    readFile();
}

void readFile() {
    // Variables
    string firstName;
    string lastName;
    double hoursWorked;
    double payRate;
    double grossPay;
    double withHolding;
    double takeHomePay;
    double overtime;
    string dataOutFileName = "Salary.out";

    // Intialize and Open Input File
    ifstream file;
    file.open(fileName);
    // Initialize Output File
    ofstream dataOutFile(dataOutFileName);

    // Check to see if file failed to open
    if (!file.is_open()) return;

    // Define variables needed in the while loop.
    string word;
    int i = 1;

    // Actually reads through the file and prints out what the file has.
    while (i != 0) {
        // Pull up the next word in the word file
        file >> word;
        // Firstname
        if (((i - 1) % 4) == 0) {
            firstName = word;
            cout << "First name: " << firstName << "\n";
        }
        // Last name
        else if (((i - 1) % 4) == 1) {
            lastName = word;
            cout << "Last name: " << lastName << "\n";
        }
        // Hours Worked
        else if (((i - 1) % 4) == 2) {
            hoursWorked = atof(word.c_str());
            cout << "Hours Worked: " << hoursWorked << "\n";
        }
        // Pay Rate
        else if (((i - 1) % 4) == 3) {
            payRate = atof(word.c_str());
            cout << "Pay Rate: " << payRate << "\n";
        }
        // Add 1 to i
        i++;
        // If i-1 divides into 4 with no remainder, move to new line
        // Also since we now have all four variables filled in we can do our math
        if (i > 3 && ((i - 1) % 4) == 0) {
            // Gross Pay
            if (hoursWorked > 40) {
                overtime = hoursWorked - 40;
            }
            else {
                overtime = 0;
            }
            if (overtime != 0) {
                grossPay = (40 * payRate) + (overtime * (payRate * overtimeRate));
            }
            else {
                grossPay = hoursWorked * payRate;
            }
            // Withholding
            if (grossPay > withHoldingLimit) {
                withHolding = grossPay * withHoldingRate;
            }
            else {
                withHolding = 0;
            }
            // Take Home pay
            takeHomePay = grossPay - withHolding;
            // Add to totals
            totalGrossPay += grossPay;
            totalWithHolding += withHolding;
            totalTakeHomePay += takeHomePay;
            // Write to file, and print the line so we know it worked!
            dataOutFile.open(dataOutFileName, fstream::app);
            // Check it if is open
            if (!dataOutFile.is_open()) {
                cout << "Couldn't open file";
            }
            else {
                dataOutFile << firstName << " " << lastName << " " << hoursWorked << " " << payRate << " " << grossPay << " " << withHolding << " " << takeHomePay << endl;
                cout << "What was wrote to file: \n" << firstName << " " << lastName << " " << hoursWorked << " " << payRate << " " << grossPay << " " << withHolding << " " << takeHomePay << endl;
            }
            dataOutFile.close();
            // move to new line
            cout << "\n";
        }
        // Check to see if were at the end of the file, if so end while.
        if (file.eof()) {
            i = 0;
        }
    }
    file.close();
}

1 个答案:

答案 0 :(得分:2)

基于i的状态机过于复杂,完全不需要。不要修理它,扔掉它。

如果您需要阅读四件事,请一次阅读四件事。在while条件下进行。

 ifstream file(filename);
 ofstream dataOutFile(dataOutFileName);

 while (file >> firstName >> lastName >> hoursWorked >> payRate)
 {
      // you have all four pieces of data
      // calculate and print what you need here
 }

请勿在循环内调用close()或检查eof()