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 :
#include <iostream>
#include <fstream>
#include <string>
usingnamespace 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;
}
elseif (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.