Simple i/o problem using txt file as input

I am working in my text book Problem Solving, Abstraction, and design using C++, 5th edition, Friedman. I am trying to learn about file i/o. My code will not compile and I am getting frustrated. Any input as to why it will not compile would be greatly appriciated. Here is my code.

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
#include <iostream>
#include <cstdlib>		// required for file streams
#include <fstream>		// for definition of EXIT_FAILURE
using namespace std;

// Associate streams with external file names
#define inFile "EmpFile.txt"		// employee file
#define outFile "Salary.txt"		// payroll file

// Functions used......
// PROCESS ALL EMPLOYEES AND COMPUTE TOTAL
float processEmp(ifstream&, ofstream&);

int main()
{
	ifstream eds;			// input: employee data stream
	ofstream pds;			// output: payroll data stream
	float totalPayroll;		// output: total payroll

	// Prepare files
	eds.open(inFile);
	if (eds.fail ())
	{
		cerr << "*** ERROR: Cannot open " << inFile
				<< " for input." << endl;
		return EXIT_FAILURE;	// failure return
	}

	pds.open(outFile);
	if (pds.fail ())
	{
		cerr << "***ERROR: Cannot open " << outFile
				<< " for output." << endl;
		eds.close();
		return EXIT_FAILURE;	// failure return
	}

	// Process all employees and compute total payroll.
	totalPayroll = processEmp(eds, pds);

	// Display results
	cout << "Total payroll is $" << totalPayroll << endl;

	// Close files
	eds.close();
	pds.close();

	system ("pause");
	return 0;

}

// Process all emplyees and compute total payroll amount
// Pre: eds and pds are prepared for input/output.
// Post: Employee names and salaries are written from eds to pds
//		and the sum of their salaries is returned.
// Returns: Total company payroll
float processEmp
	(ifstream& eds, 		// IN: employee file stream
	 ofstream& pds)			// IN: payroll file stream
{
	string name;		// input: first name
	// string lastName;		// input: last name
	float hours;			// input: hours worked
	float rate;				// input: hourly rate
	float payroll;			// return value - total company payroll

	payroll = 0.0;
	// Read first employee's data record.
	getline(eds, name);
	// eds >> firstName >> lastName >> hours >> rate;
	while (!eds.eof())
	{
		// Get current employee salary data.
		eds >> hours >> rate;
		salary = hours * rate;
		pds << name << " " << salary << endl;
		payroll += salary;
		// Read next employee's data record.
		getline(eds, name;
		// eds >> hours >> rate;
	}	// endl while

	return payroll;
}  // end processEmp 
D:\prog\cc\foo\a>g++ -Wall a.cpp
a.cpp: In function 'float processEmp(std::ifstream&, std::ofstream&)':
a.cpp:76: error: 'salary' was not declared in this scope
a.cpp:80: error: expected `)' before ';' token

You need to declare salary before you can use it.
Function argument lists must be terminated by a ')' character.

Make sure you compile with all warnings turned on.
Hope this helps.
I probably would have seen the two errors you listed, but VS 2008 keeps complaining about the 'getline' on line 70 above. Am I not calling a library?
 
#include <string> 

for getline()
Heh, I missed that one...
I knew it was something like that. Thanks. Now on to the next big thing. I am stepping through, it opens my text file ok, reads string value 'name' and reads the hours and rate. It even created the output file salary.txt. The problem now it it just sits in a never ending loop and nothing is getting written in the out file. I watch my veriables and I get correct values. Here is the whole of my text file. named empFile.txt

Jim Baxter
35.5 7.25
Adrian Cybriwsky
40 6.50
Ayisha Mertens
20.0 8.00

, if you would like to venture on...... You guys are awsome!

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
#include <iostream>
#include <cstdlib>		// required for file streams
#include <fstream>		// for definition of EXIT_FAILURE
#include <string>

using namespace std;

// Associate streams with external file names
#define inFile "EmpFile.txt"		// employee file
#define outFile "Salary.txt"		// payroll file

// Functions used......
// PROCESS ALL EMPLOYEES AND COMPUTE TOTAL
float processEmp(ifstream&, ofstream&);

int main()
{
	ifstream eds;			// input: employee data stream
	ofstream pds;			// output: payroll data stream
	float totalPayroll;		// output: total payroll

	// Prepare files
	eds.open(inFile);
	if (eds.fail ())
	{
		cerr << "*** ERROR: Cannot open " << inFile
				<< " for input." << endl;
		return EXIT_FAILURE;	// failure return
	}

	pds.open(outFile);
	if (pds.fail ())
	{
		cerr << "***ERROR: Cannot open " << outFile
				<< " for output." << endl;
		eds.close();
		return EXIT_FAILURE;	// failure return
	}

	// Process all employees and compute total payroll.
	totalPayroll = processEmp(eds, pds);

	// Display results
	cout << "Total payroll is $" << totalPayroll << endl;

	// Close files
	eds.close();
	pds.close();

	system ("pause");
	return 0;

}

// Process all emplyees and compute total payroll amount
// Pre: eds and pds are prepared for input/output.
// Post: Employee names and salaries are written from eds to pds
//		and the sum of their salaries is returned.
// Returns: Total company payroll
float processEmp
	(ifstream& eds, 		// IN: employee file stream
	 ofstream& pds)			// IN: payroll file stream
{
	string name;		// input: first name
	// string lastName;		// input: last name
	float hours;			// input: hours worked
	float rate;				// input: hourly rate
	float payroll;			// return value - total company payroll
	float salary;			// output: employee salary

	payroll = 0.0;
	// Read first employee's data record.
	getline(eds, name);
	// eds >> firstName >> lastName >> hours >> rate;
	while (!eds.eof())
	{
		// Get current employee salary data.
		eds >> hours >> rate;
		salary = hours * rate;
		pds << name << " " << salary << endl;
		payroll += salary;
		// Read next employee's data record.
		getline(eds, name);
		// eds >> hours >> rate;
	}	// endl while

	return payroll;
}  // end processEmp 

The problem is with
 
while (!eds.eof())


You are only checking for the end of file, you aren't checking for errors. So the file enters an error state because the second time it hits getline(), its getting the carriage return from the line with your number data on it because you retrieved them with the >> operator. So then you try and put the second person's first and last name into hours and rate (respectively) which puts the stream into an error state, and it just sits there because you only check for eof. Now you have an infinite loop. Put your original line (the one from outside the loop)

 
getline(eds, name);


inside the while condition so it reads

 
while ( getline(eds, name) ) // you can now get rid of your original getline() statement 


then your getline() inside the loop can become

 
eds.ignore() //to throw away the thing sending the stream into a failed state 


Now this will continue the loop so long as it is able to accomplish a getline() from the stream, whether or not it stops from eof, fail, or bad. And the carriage return is ignored, so it doesn't cause an issue.

Last edited on
This did the trick.
This book has some issues. I typed exactly as it stated, it did not include <stream>, but does include <fstream>.
My text file is set up exactly as directed, yet my program ran infinitely.
Just some of the challenges of taking online classes.

Thanks all!
Topic archived. No new replies allowed.