繁体   English   中英

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

[英]C++ Not inserting first line of data to output file

我是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 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;
    }

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

在此处输入图片说明

因此,这告诉我的是,它正在到达代码写入文件的位置,因为它正在写入第一个条目之后的其他四个条目。 但是,根据输出窗口,它正在将文件应有的信息写入文件,但是由于某种原因,它不是。 我正在使用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();
}

基于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()

暂无
暂无

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

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