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