Operating with Files

I am writing a payroll program that pulls information from a file, calculates pay and taxes, and outputs that information to the console. For some reason I cannot get the program to perform any calculations. I tried adding braces where I thought they were needed and my program crashed because variables weren't initialized. When I assigned values to them (0) it simply did the multiplication with zeroes instead of reading info from the file. I verified that it is opening the file. Any help would be greatly appreciated. I am really struggling with this program. Here is an example of my text file. Values are: empID, paycode, hours, rate.
101456 H 20 6.57
100045 S 81994.12
100321 H 45 23.50
101987 H 39 15.76
100486 S 116935.65

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <cmath>
#include <fstream>
using namespace std;

int main()
{

//Variables
double grosspay;
double overtimepay;
double netpay;
double taxes;
int empID;
char code;
double hours;
double rate;
double OThours;
OThours = 0;
//Open File

ifstream fileIn;

//inFile.open ("payData.txt");
fileIn.open ("payData.txt"); //Open file

    if (fileIn.fail() )             // Test for file existence 
    { 
      cout <<  "Problem opening file" << endl; 
      exit(-1); 
    } 

//code = 'a';
//rate = 0;
//hours = 0;


while (!fileIn.eof())           // While not at end-of-file

fileIn >> empID, code;

if (code == 'S')

	grosspay = grosspay / 52;


if (code == 'H')
	if (hours <= 40)
	
		grosspay = hours * rate;
	
	else OThours = hours - 40;
		grosspay = (OThours * rate * 1.5) + ((hours - OThours) * rate);


//Tax calculations

if (grosspay < 925)
	netpay = grosspay * 0.85;

else if (grosspay >= 925 && grosspay < 2860)
	netpay = (grosspay - 925) * 0.72 + 138.75;

else if (grosspay >= 2860 && grosspay < 4100)
	netpay = (grosspay - 2860) * 0.68 + 680.55;

else if (grosspay > 4100)
	netpay = grosspay * 0.60;

cout << "pay" << grosspay << " " << netpay << endl;

system ("Pause");
return 0;
}
Last edited on
1. Read about compound statements here: http://www.cplusplus.com/doc/tutorial/control/
2. fileIn >> empID, code; should be: fileIn >> empID >> code;
3. Where do you read in grosspay or hours and rate?
As I was taught the program moves from one value to the next in sequential order. So what I was going for was that if the code was H, the program would read the next two values as hours and rate. Which would then calculate the grosspay (line 50). Unfortunately I am very lost and can't seem to get this figured out. I tried some other things, but now my program is just going into an infinite loop and displays a seemingly random number. Here is where I think my problem lies, it wont seem to move on through the file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
fileIn >> empID;
fileIn >> code;
fileIn >> hours;
fileIn >> rate;
fileIn >> salary;

while (!fileIn.eof())           // While not at end-of-file
{


if (code == 'S')
	fileIn >> salary;
	grosspay = salary / 52;

if (code == 'H')
	fileIn >> hours >> rate;

	if (hours <= 40)
	
		grosspay = hours * rate;
	
	else OThours = hours - 40;
		grosspay = (OThours * rate * 1.5) + ((hours - OThours) * rate);

cout << hours << endl << grosspay;
}
Last edited on
I tried adding braces where I thought they were needed

Sorry I guess that I skimmed over the above on first read.

You definitely need braces. For example:
1
2
3
while (!fileIn.eof())           // While not at end-of-file

fileIn >> empID >> code;
The above will either read the whole file before exiting the while loop or the stream will possibly enter an error state and get stuck in the loop.

Let's see what your code looks like with the braces back in..


Edit: Had a bunch of tabs open and missed your edit.

Try it like this:
1
2
3
4
5
6
7
8
9
10
11
//fileIn >> empID;
//fileIn >> code;
//fileIn >> hours;
//fileIn >> rate;
//fileIn >> salary;

while (!fileIn.eof())           // While not at end-of-file
{
fileIn >> empID >> code;
if (code == 'S')
. . .


Edit 2: You also need braces on the if(s) and the else.
Last edited on
Finally, thank you so much. I have one last minor question. I know this is asked all the time, but the FAQ's still have me a little confused. I have the
while (!fileIn.eof()) command but yet it is still processing the last line in my text file twice. What am I doing wrong?
You probably have a blank line at the end of your data file.

Try this:
1
2
3
fileIn >> empID >> code;
if(fileIn.fail()) break;
if (code == 'S')
Awesome, I really appreciate all the help that you've given me when you didn't have to. It worked great and now I have a better understanding of how reading files works than I had before (not to mention a working program). Thanks again have a good night...or morning depending on where you are.
Topic archived. No new replies allowed.