我對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
它完全跳過寫出關於喬恩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;
}
因爲我的輸出窗口如下所示:
那麼,告訴我的是,它越來越向代碼寫入文件的位置,因爲它正在寫入第一個條目之後的其他四個條目。但是,根據輸出窗口,它正在向文件寫入它應該的信息 - 但由於某種原因,它不是。我使用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();
}
解決此類問題的正確工具是您的調試器。在*堆棧溢出問題之前,您應該逐行執行您的代碼。如需更多幫助,請閱讀[如何調試小程序(由Eric Lippert撰寫)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。至少,您應該\編輯您的問題,以包含一個[最小,完整和可驗證](http://stackoverflow.com/help/mcve)示例,該示例再現了您的問題,以及您在調試器。 –
[爲什麼file.eof()總是出錯](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Blacktempel
@πάνταῥεῖ我說我通過我的調試日誌,它運行時沒有錯誤或警告。我還能做些什麼?我提供了每一步來重現我的問題。從我用於數據的文本文件,到它正在呈現的輸出以及我所期望的內容。這是一個非常明確的問題,我的代碼被註釋掉了,所以我知道每一步都在做什麼。我不確定你眼中的問題有什麼問題,但是我儘可能地提出了最好的方案。 – user6829333