Finding the highest number from a text file

Hello,

I have a piece of coursework that I am working on, I have to take in certain inputs from a text file which describe employees and the hours they work, i have to display them in an orderly fashion, then work out the total wage cost for each employee per week, then the combined wage cost of all the employees. I have done all of this, but i am stuck at the last bit. At the start of the program the user has to input a 'Recommended amount of wage costs' and i have to take this and output the shop with the highest total wage costs whilst being lower the recommended amount. At the moment i have this as 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
86
87
88
89
90
91
92
93
94
95
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main (){
	string unit, no;
	float s, hours, rates, weeklywage, x, RM, totalwage =0, a, largesttotal;

	fstream read, write;
	read.open("input.txt", ios::in);
	write.open("output.txt",ios::out);

	if (write.fail()){
		cerr << "The input file has failed to open" << endl;
	}
	else{
		cout << "The input file has opened correctly" << endl;
	}

	if (read.fail()){
		cerr << "The output file has failed to open " << endl;
	}
	else {
		cout << "The output file has opened correctly" << endl;
	}



	if (read.is_open()){
		cout << "Please enter a recommended staff wage cost ";
		cin >> RM;
	

		while (!read.eof()){
			
			cout << "_____________________________________________________" << endl;
			read >> unit >> no;
			cout << unit << " " << no << endl << endl;
			read >> s;
			cout << "No. of sale assistants: " << s << endl << endl;
			totalwage = 0;
			largesttotal = 0;
			a=0;

			for (x=1; x<=s; x++){

				read >> hours >> rates;
				weeklywage = (hours*rates);

				a = a+1;
				cout << "Employee: " << a << endl;
				cout << "Hours worked: " << hours << endl;
				cout << "Hourlywage: " << rates << endl;
				cout << "Weekly Wage: " << weeklywage << endl << endl;
				totalwage = totalwage + weeklywage;
				


			}
			cout << "_____________________________________________________" << endl;

			cout << "Units total wage cost: " << totalwage << endl;

			if (totalwage < RM) {
				write << unit << no << endl << "This units costs are under the recommended amount " << endl << endl;
				cout << "This units costs are under the recommended amount " << endl;
				cout << "_____________________________________________________" << endl;
			}

			else if (totalwage > RM ){
				cout << "This units costs are over the recommended amount" << endl;
				write << unit << no << endl << "This units costs are over the recommended amount" << endl;
				cout << "_____________________________________________________" << endl;
			}
			else {
				cout << "This units costs is the same as the recommened amount " << endl;
				write << unit << no << endl << "This units costs is the same as the recommened amount " << endl;
				cout << "_____________________________________________________" << endl;
			}

				
		if (totalwage > largesttotal && totalwage < RM)
			largesttotal = totalwage;

		}

		cout << "This may work " << largesttotal << endl;
	}
	read.close();
	write.close();
	system ("pause");
	return 0;
}



And this is the text file that has the data:
Unit One
4
32 8
38 6
38 6
16 7

Unit Two
0

Unit Three
2
36 7
36 7

Unit Four
6
32 6.5
32 6.5
36 6.5
36 6.5
38 6.5
38 6.5

Unit Five
4
32 6.5
32 8
32 7
32 8

Unit Six
5
38 7
30 6.5
24 8
24 8
24 8

Unit Seven
0

Unit Eight
1
40 12

Unit Nine
5
24 8
24 6.5
30 6.5
24 7
32 7

--------

My problem is that when i am using my if statement at the end to find out the unit with the highest cost under the recommended i am only getting the last total wage cost. But i cant figure out why, please help.
Last edited on
Topic archived. No new replies allowed.