简体   繁体   English

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

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

I'm relatively new to C++, and have been working my way through taking a data file and reading it, then taking that data doing some math with some of it, and then exporting it all to an output file. 我是C ++的新手,一直在努力获取数据文件并读取它,然后对该数据进行一些数学运算,然后将其全部导出到输出文件中。 However my problem is everytime it runs it skips writing the very first line to the output file. 但是我的问题是,每次运行它都会跳过将第一行写入输出文件的过程。

My data file is: 我的数据文件是:

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

My output looks like this: 我的输出如下所示:

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

It's completely skipping writing out any information concerning Jon Doe. 它完全跳过了写有关乔恩·多伊的任何信息。 Which is: 这是:

Jon Doe 15 7.25 108.75 0 108.75

I've tried multiple different ideas, asked my friends, and overall I'm extremely frustrated. 我尝试了多种不同的想法,问我的朋友,总的来说,我感到非常沮丧。

I figure the problem code is in here somewhere: 我认为问题代码在这里的某个地方:

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

Because my output window looks like this: 因为我的输出窗口如下所示:

在此处输入图片说明

So what that tells me is that it is getting to the code where it writes to the file because it's writing the other four entries past the first one. 因此,这告诉我的是,它正在到达代码写入文件的位置,因为它正在写入第一个条目之后的其他四个条目。 But also according to the output window it is writing to the file the information that it should- but for some reason it isn't. 但是,根据输出窗口,它正在将文件应有的信息写入文件,但是由于某种原因,它不是。 I'm using the append command so it shouldn't be overwriting anything, and according to the output file it isn't but maybe the first line. 我正在使用append命令,因此它不应覆盖任何内容,根据输出文件,它不是,而是第一行。

No errors, or warnings, and my debug logs are free of errors. 没有错误或警告,并且我的调试日志中没有错误。 Please help me, any help is appreciated. 请帮助我,任何帮助表示赞赏。 I also realize the code is kinda messy, and I need to break it down into more functions but I'm just trying to get this to work, and then I can clean it up. 我也意识到代码有点混乱,我需要将其分解为更多的功能,但是我只是想使其正常工作,然后我才能对其进行清理。

The full code for my program that handles all of this is below, in case anyone needed to see it. 下面是我处理所有这些程序的完整代码,以防有人需要查看。

#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();
}

The i-based state machine is way too complex and totally not needed here. 基于i的状态机太复杂了,这里完全不需要。 Don't fix it, throw it away. 不要修复它,把它扔掉。

If you need to read four things, read four things at once. 如果您需要阅读四件事,请一次阅读四件事。 Do it inside the while condition. 在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
 }

Do not call close() or check for eof() inside the loop. 不要在循环内调用close()或检查eof()

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

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