The assignment requires program to read from a file into an array, use two boolean functions to validate certain data (set to "true" when there are no errors) then output any errors to a separate file. Everything works fine except for validating the rate. All the numbers are read into the array correctly and the calculation seems sound (works correctly on a calculator) but for some reason the calculation in C++ always results in 0 which makes EVERY record output an error. PLEASE HELP, it is driving me CRAZY :twisted: (the "cout" lines were inserted just so I could see what was happening. They will not be part of the final code; I've successfully complied the portion to validate the labor force but removed it to isolate the rate validation portion.)
#include <iostream>
#include <fstream> //allows the program to read from & output to separate files
#include <cstdlib>
#include <string>
using namespace std;
//Declarations
int year[126];
string month[126];
int laborForce[126];
int numberOfEmployed[126];
int numberOfUnemployed[126];
double Rate[126];
double Rate1;
int LF1;
int i;
int employData = 0;
string mainTitle = " Error Log for Employment File\n\n";
string errorData = "There is an error in the following record:\n";
bool validLaborF()
{
if(numberOfEmployed[i] + numberOfUnemployed[i] == laborForce[i])
return true;
}
bool validRate()
{
if(Rate[i] == (numberOfUnemployed[i] / laborForce[i]) * 100)
return true;
}
int main()
{
ifstream indata; //input file stream
ofstream outdata; //output file stream
//open needed files
indata.open("employment2.txt");
outdata.open("error-log.txt");
outdata << mainTitle << endl;
indata >> year[i] >> month[i] >> laborForce[i] >> numberOfEmployed[i] >> numberOfUnemployed[i] >> Rate[i];
while (indata && employData < 127)
{
indata >> year[i] >> month[i] >> laborForce[i] >> numberOfEmployed[i] >> numberOfUnemployed[i] >> Rate[i];
Rate1=(numberOfUnemployed[i] / laborForce[i]) * 100;
LF1=(numberOfEmployed[i] + numberOfUnemployed[i]);
cout << LF1 << "\t\t" << laborForce[i] << "\t\t" << numberOfUnemployed[i] << "\t\t" << Rate[i] << "\t\t" << Rate1 << endl << endl;
if (validRate())
indata >> year[i] >> month[i] >> laborForce[i] >> numberOfEmployed[i] >> numberOfUnemployed[i] >> Rate[i];
else
outdata << errorData << year[i] << "\t" << month[i] << "\t\t" << laborForce[i] << "\t\t" << numberOfEmployed[i] << "\t\t" << numberOfUnemployed[i] << "\t\t" << Rate[i] << endl << endl;
}
//Close all files
indata.close();
outdata.close();
cout << "\nEnd of report" << endl << endl;
return 0;
}