Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
1.89/5 (2 votes)
See more:
So I have an assignment where I have to use arrays and output data from an input file.

It use to run, but I made changes and now it will run, but it won't read from the data file. I'm not sure what changes I made, so I am lost as to fix it. Someone told me to rearrange the calculations and taxes and grosspays, which is the change I made. But now, even if I go back to what I had before, it will not run properly.

I am using visual studio 2015


C++
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
int main() {

	ifstream fin("employee.txt");
	char SMH; //Single, Married, or Head of Household
	string firstname[10], lastname[15];
	int employeeid[100], i = 0;
	int hoursworked[100], overtimehours[100];
	double hourlyrate[100], regularpay[100];
	double overtimepay[100], grosspay[100], taxrate, taxamount, netpay;
	int counter = 0;

	cout << "                                      EBRAHIMI PAYROLL INSTITUTE" << endl;
	cout << "                                              106 Easy Ways" << endl;
	cout << "                                       PLEASENTVILLE, N.Y. 11068" << endl;

	while (fin >> employeeid[i] >> firstname[10] >> lastname[15] >> hoursworked[i] >> hourlyrate[i] >> SMH) {
		counter = counter + 1;
		for (i = 0; i < counter; i++) {
			if (hoursworked[i] > 40) {
				overtimehours[i] = hoursworked[i] - 40;
				overtimepay[i] = overtimehours[i] * hourlyrate[i] * 1.5;
				regularpay[i] = 40 * hourlyrate[i];
			}//if 
			else {
				overtimehours[i] = 0;
				overtimepay[i] = 0;
				regularpay[i] = hoursworked[i] * hourlyrate[i];
			}//else
		}//for
		for (i = 0, i < counter; i++;) {
			grosspay[i] = hourlyrate[i] + overtimepay[i];
		}//end grosspay for overtimepay loop

		for (i = 0; i < counter; i++) {
			if (grosspay[i] > 1000)   taxrate = 0.30;
			else if (grosspay[i] > 800)   taxrate = 0.20;
			else if (grosspay[i] > 500)   taxrate = 0.10;
			else taxrate = 0.0;
		}//for end of grosspay and set taxrate loop

		switch (SMH) {

		case'S': taxrate += 0.05;
			break;
		case'M': taxrate += 0.10;
			break;
		case'H': taxrate -= 0.05;
			break;
		}//switch

		for (i = 0; i < counter; i++) {
			taxamount = grosspay[i] * taxrate;
		}// end of taxamount loop
		for (i = 0; i < counter; i++) {
			netpay = taxamount - grosspay[i];
		}//end of netpay loop


		cout << setw(14) << "Employee Id" << setw(16) << "First Name" << setw(15)
			<< "Last Name" << setw(6) << "Stat" << setw(6) << "HW" << setw(6) << "HR" << setw(8) << "Gross" << setw(6)
			<< "Tax" << setw(9) << "RegP" << setw(6) << "OTP" << setw(9) << "Netpay" << endl << endl;

		for (i = 0; i < counter; i++) {
			cout << setw(14) << employeeid[i] << setw(16) << firstname[10] << setw(15) << lastname[15] << setw(6) << SMH
				<< setw(6) << hoursworked[i] << setw(6) << hourlyrate[i] << setw(8) << grosspay[i] << setw(6)
				<< taxrate << setw(9) << regularpay[i] << setw(6) << overtimepay[i] << setw(9) << netpay << endl;
			i = i + 1;
		}//for
	}//WHILE
	fin.close();
	system("pause");
	return 0;
}//MAIN



This is the assignment in case my question doesn't deliver the context well:

DR. EBRAHIMI'S PAYROLL INSTITUTE

FIRST NAME	LAST NAME	STAT	SSN HW HR OTH OTP REGP GROSS TAX NET


 John	Smith	M	 113 50 20 10 300 800 1100 385 715

 Jane	Dow 	M	 223 40 15 5 112.5 675 787.5 275 512.5


(The above is supposed to be a table format. It didn't copy well)

B) Take advantage of arrays by breaking programs into separate units. Each unit should have a separate loop. (Do not use functions.)

Read all data into arrays
Compute all the overtimepays
Compute all the grosspays
Compute all the taxrates
Compute all the netpays

Display all the arrays
C) Include separate functions to read in the data, compute the gross pay, tax rate, net pay, and overtime pay for all employees, and display.
Posted
Updated 12-Aug-15 13:57pm
v2

First of all, "doesn't work properly" is not a useful error description. How are we supposed to help if you don't even tell us what's wrong, and what you expect?

Second, you should always check the state of a stream you are reading from, preferably before you read from it. E. g. call is_open(), good() (or bad()), and eof() at the appropriate points in your program to ensure the stream is in the state you expect. That way you can be sure when things go south. See http://www.cplusplus.com/doc/tutorial/files/[^]

Third, what your input stream reads depends entirely on the exact content and format of the input file. If you mix up the order of values or start at the column headings rather tahn the first line of data, this of course won't work as expected! Therefore, rather than directly streaming values into your local variables it may be better to simply read each line with getline(), check whether it contains any data, and then use std::strstream to convert the line into values.
 
Share this answer
 
My solution would be to run the program in debugger mode and watch variables.
You should see very quickly what your program is really doing.

From the code I see, the problem is certainly related to the data file you are reading, and the way you are reading it. Since you are the only one to have that file, we can't check this.
If your reading commends leave a 0 to the while, it will abort the program.
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900