Using ifstream with functions

Hi everyone,

I can't seem to figure this out. I'm trying to get data from a file (lines 46-48) and then use that data in some functions. This is working out fine except when I try to loop it to get a new set of data. My program seems to continue to use the old data, even though the loop is in place.

Any suggestions?

Thanks!
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>


using namespace std;

float grosspay(float hours, float payRate);
float otgross(float hours, float payRate);
float totalDeductions(float gross, float dependents, float retirement);
void drawLine();

// Named constant definitions (and function declarations):

	const float taxes = .23;
	const float social = .08;
	const float insurance = 12;
	
	float gross, deductions;

	ifstream indata;
	
// Main program: 

int main() 
{
	float hours, payRate, net, dependents, retirement;
	string name;
	char e;
	int count;
	indata.open("Pay.dat");
	
	cout << showpoint << fixed;
//get name from file

//loop
while(!indata.eof())
{
	//get name, prompt for hours
	getline(indata,name);
	cout << "How many hours did " << name << " work?" << endl;
	cin >> hours;
	
//get payrate, dependents, retirement from file
	indata >> payRate;
	indata >> dependents;
	indata >> retirement;
//run functions
	grosspay(hours, payRate);
	otgross(hours, payRate);
	totalDeductions(gross, dependents, retirement);

//clear screen, calculate net, output data
	system("cls");
	net = gross - deductions;
	drawLine();
	cout << setprecision(2);
	cout << "Name:" << right << setw(26) << name << endl;
	cout << "Gross Pay:" << right << setw(15) << "$" << gross << endl;
	cout << "Total Deductions:" << right << setw(8) << "$" << deductions << endl;
	cout << "Net Pay:" << right << setw(17) << "$" << net << endl;
	drawLine();
	drawLine();
	
//prompt to exit or continue
	cout << "Press 'c' to request next person or 'e' to exit." << endl;
	cin >> e;
	if(e == 'e')
		exit(1);
}
return 0;
} //end function main

//Variable declarations:


float grosspay(float hours, float payRate)
{
	gross = hours * payRate;
	
	return gross;
}

float otgross(float hours, float payRate)
{

//if hours > 40 find overtime
	if(hours > 40) 
	{
		gross = (gross - ((hours - 40) * payRate)) + ((hours - 40) * (payRate * 1.5));
		return gross;
	}
	
	else
		return gross;

}

float totalDeductions(float gross, float dependents, float retirement)
{
	float taxAmount, ssAmount, insAmount, retireAmount;

//calculate taxes, social, insurance, retirement amounts
	taxAmount = taxes * gross;
	ssAmount = social * gross;
	insAmount = 12 * dependents;
	retireAmount = (retirement / 100) * gross;
	
//find total
	deductions = taxAmount + ssAmount + insAmount + retireAmount;
	
	return deductions;
}

void drawLine()
{
	cout << "===============================" << endl;
}


And this is the data file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
John W. Smith
12.55
3
5
Mary Anderson
11.75
1
8
Brad W. Baker
11.75
0
0
Heather Johnson
13.25
2
10
After you read the first guy's retirement with indata >> retirement; the end of line is not yet processed.
The next thing you do with that file (beside the erroneous "while not eof") is getline(indata, name). That getline sees the endline and stores an empty string in "indata".
The next thing you do with that file is indata >> payRate;, which sees the letter 'M', cannot store that in the variable payRate, and sets the error flag on your stream.
No further input takes place, which you see as "continue to use the old data,"

At the very least, consume that endline after indata >> retirement (e.g. by calling indata.ignore();)
Thanks a lot, I appreciate the help!

There's also one other thing -
I'm not very familiar with setw(), but I'm trying to align all the names, gross pay, deductions, and net pay amounts on the right (so they end at the end of the = signs).

Google isn't seeming to help me and I feel like I've tried everything.

Any ideas?
Did you try just a simple tab? "\t";
Well that wouldn't right align it (guess I should have made that a little more clear). I need the amounts and names right aligned.
I've read those and I don't understand what I'm doing wrong. Isn't my code right? shouldn't cout << "Name:" << right << setw(26) << name << endl; right align the name string?
it certainly should: http://ideone.com/wdLcIZ

Topic archived. No new replies allowed.