My assignment is to create a program computing all kinds of employee data. Just beginning, trying to put together the basis for the rest of it, here's my code:

#include <iostream>
#include <cmath>
#include <iomanip>
#include <fstream>
using namespace std;

int GrossPay(int,int);
double Tax(int);

int main ()
{
	ifstream empdata;
	empdata.open("employees");
	
	int totEmpl, ID, hrlPay, hrsWrk;
	int GrP = 0;
	double Tx = 0;
	int Net = GrP-Tx;
	
	cout.setf(ios::fixed,ios::floatfield);
	cout << setw(2) << "#" << "\t" << setw(5) << "ID" << "\t"
		<< setw(5) << "$/Hr" << "\t" << setw(5) << "#/Hrs" << "\t"
		<< setw(5) << "Gross" << "\t" << setw(5) << "Tax" << "\t"
		<< setw(5) << "Net" << endl << endl;
	
	empdata >> totEmpl;
	
	for (int count=1; count<=totEmpl; count++) {
		empdata >> ID >> hrlPay >> hrsWrk;
		GrP = GrossPay(hrlPay,hrsWrk);
		Tx = Tax(GrP);
		cout << setw(2) << count << "\t" << setw(5) << ID << "\t"
			<< setw(5) << hrlPay << "\t" << setw(5) << hrsWrk << "\t"
			<< setw(5) << GrP << "\t" << setw(5) << Tx << "\t"
			<< setw(5) << Net << endl << endl;
	}
	
	
	
	empdata.close();
	return 0;
}

int GrossPay (int r, int w) {
	if (w<=40)
		return r;
	else return r*2;
}

double Tax (int gp) {
	char tax;
	if (gp>500)
		tax=10;
	else if (gp>=200 && gp<=500)
		tax=5;
	else tax=0;
}

Why won't it work.

What's the value of totEmpl after the headings are printed? Print it out.

Edited 5 Years Ago by WaltP: n/a

What's the value of totEmpl after the headings are printed? Print it out.

It's 0; it's not reading the file. Here's the contents of employees.txt:

12
283 20.00 5
156 50.00 20
385 15.00 60
739 100.00 20
555 50.00 50
412 100.00 1
904 150.00 8
716 75.00 3
872 35.00 70
202 20.00 50
194 200.00 10
810 40.00 4

(Edit: Every line following the first one consists of three numbers, separated by tabs. The first column is the employee ID, second is hourly rate of pay, third is hours worked.)

The example given in class read the file the way I coded it to... and the file was the exact same format (the top number was the number of how many lines to follow, and the lines to follow were groups of three also, to check if they made triangles...). I don't exactly understand how this is supposed to work though, reading the first line and then the following ones? Maybe I understood what he was writing wrong? but he wrote it all out on the board, the entire code, and that's all there was, that's how he wrote it. So I don't know. Let me know.

Edited 5 Years Ago by caltech: n/a

Oh boy...

Here's my altered program code. I got it to read the file, I guess, or at least open it, by inserting the whole file location (taken out here).

#include <iostream>
#include <cmath>
#include <iomanip>
#include <fstream>
using namespace std;

int GrossPay(int,int);
double Tax(int);

int main ()
{
	ifstream empdata;
	empdata.open("ENTIRE FILE LOCATION.txt");
	
	int totEmpl, ID, hrlPay, hrsWrk;
	int GrP = 0;
	double Tx = 0;
	int Net = GrP-Tx;
	
	cout.setf(ios::fixed,ios::floatfield);
	cout << setw(2) << "#" << "\t" << setw(5) << "ID" << "\t"
		<< setw(5) << "$/Hr" << "\t" << setw(5) << "#/Hrs" << "\t"
		<< setw(5) << "Gross" << "\t" << setw(5) << "Tax" << "\t"
		<< setw(5) << "Net" << endl << endl;
	
	empdata >> totEmpl;
	
	for (int count=1; count<=totEmpl; count++) {
		empdata >> ID >> hrlPay >> hrsWrk;
		GrP = GrossPay(hrlPay,hrsWrk);
		Tx = Tax(GrP);
		cout << setw(2) << count << "\t" << setw(5) << ID << "\t"
			<< setw(5) << hrlPay << "\t" << setw(5) << hrsWrk << "\t"
			<< setw(5) << GrP << "\t" << setw(5) << Tx << "\t"
			<< setw(5) << Net << endl << endl;
	}
	
	empdata.close();
	return 0;
}

int GrossPay (int r, int w) {
	if (w<=40)
		return r;
	else return 2*r;
}

double Tax (int p) {
	char t;
	if (p>500)
		t=10;
	else if (p>=200 && p<=500)
		t=5;
	else t=0;
	return t;
}

So now for some reason it seems to read totalEmpl correctly (12, I checked at the end before), and count fine... However, it seems to be printing the first line twelve times, and for some reason it starts messing up at Hours Worked instead of the calculated parts/function calls. What's wrong!!!!! ........


# ID $/Hr #/Hrs Gross Tax Net

1 283 20 -1881143876 20 0.000000 0

2 283 20 -1881143876 20 0.000000 0

3 283 20 -1881143876 20 0.000000 0

4 283 20 -1881143876 20 0.000000 0

5 283 20 -1881143876 20 0.000000 0

6 283 20 -1881143876 20 0.000000 0

7 283 20 -1881143876 20 0.000000 0

8 283 20 -1881143876 20 0.000000 0

9 283 20 -1881143876 20 0.000000 0

10 283 20 -1881143876 20 0.000000 0

11 283 20 -1881143876 20 0.000000 0

12 283 20 -1881143876 20 0.000000 0

Okay... Fixed that pretty much... The periods/decimals (.00) in the text file were screwing everything up apparently. That's what we were supposed to have for the text file though, exactly, technically... but Teach'll deal...

Do you know the difference between 10 and 10.00? What type of values they are? And what type of variables are needed to hold them?

uh, yeah, cool, smart ass. 10.00 would be a double or float, not an integer, i know that much. i declared them as such. i didn't think about it because that's just how they were presented to us for the text file we were supposed to create, which was supposed to be int int int whatever. no need for the loser attitude Walt

I mean I declared the tax as double.... because it's supposed to be in the end...

Got the GrossPay function working just great too. (Asshole)

This article has been dead for over six months. Start a new discussion instead.