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

Recommended Answers

All 4 Replies

You are calculating one int divided by another. In C++, dividing one int by another will return an int.

So, for example, 5/2 = 2, and 3/4 = 0.

If you want the output to not be an integer, don't divide one integer by another. One way to achieve this is to cast one of the integers to a float. For example,

int x=5;
int y=4;

cout << x/y;  // gives 1
cout << float(x)/y; // gives 1.25

I see that part of your code relies on comparing two numbers, at least one of which is a double. You need to understand why this is fraught with danger: http://floating-point-gui.de/ , and in particular http://floating-point-gui.de/errors/comparison/

That is awesome!! Thanks so much, Moschops, you ARE a posting whiz! In the original data file, the teacher said Rate was declared as a double and the others as integers; I just assumed we were supposed to do the same (yes, I know what they say about when you "assume" :smile: ) Thanks for the helpful links.

I have just one more request for help if you don't mind...is there a way to have the calculated rate round to the tenths place so it will exactly match the rate listed in the indata?
example: indata outdata
4.1 4.14591 shows on error log but shouldn't be an error
4.2 4.17198 same thing, but not technically an error

THANKS SO MUCH!!!!!

Forcing rounding to tenths; try something like

float rounded = floorf(val * 10 +  0.5) / 10;

Finally got everything to work together. Final code is awesome!! Thanks so much, Moschops, for taking the time to help me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.