Hi I have university coursework due for next week on iterative Statements and nested loopd and I've really been struggling getting started. Once I get through the first task I'm confident I know how to do the rest but I'm really stuck.
The part of my assignment I'm stuck on is:
A wages calculator application is required that can determine the cost of employing the sales assistants. A file containing pay details for each sales assistant in each shop unit is provided to you, in the following format:
<shop unit>
<s> // where <s> is the number of sales assistants
<hours> <rate> // for each s, <hours> is hours worked per week and <rate> is hourly rate
Your application will calculate the total staff cost of each unit per week and then compare it with a ‘recommended maximum staff cost’ (RM) amount typed in by the user.
Here is a sample of the data:
Unit One
4
32 8
38 6
38 6
16 7
Unit Two
0
Unit Three
2
36 7
36 7
What exactly I am wondering is if there is a way to create a loop to calculate the total staff cost for each unit?
Currently once I open my file using infile.open("values.txt") I am then declaring each value to a variable, for example:
string name;
int total;
int staff; //no of sales assistants
int a1; //hours
int a2; //rate
int a3; //hours
I have dones this for all values then declared them by using;
infile>>name>>staff>>a1>>a2>>a3...
and then calculated the total by just doing total = (a1*a2)+(a3*4).. etc for each unit.
This seems extremely long winded and I am almost positive it's not the way I should be doing it. I was wondering if there is any way you could write a loop to read through the file and calculate the total for each unit. All I am hoping for is for someone to point me in the right direction. Sorry if this wasn't well explained I have only been using C++ for a couple of months and I've really been struggling.
So you have a single file that holds all the data, i.e. holds data for all members of staff. In which case you read through the file, and when your code identifies a new staff member it creates a new instance of a staff member. So, I suggest that you write a class or struct to hold the data per member of staff and then store each staff member instance in a vector. Get each member of staff instance to return the total cost for itself. Finally, iterate over the vector of all members of staff and sum the total cost of all members.
So to solve a complex problem you break the problem down into small units.