Having trouble with if else statement

Im having trouble with an if else statement
I have a documentation and it says this:
Locations 100-199 get paid $10.00 per hour with time and a half for any hours over 5 per day.
Locations 200-299 get paid $7.50 per hour with double time for hours worked over 6 per day.
Locations 300-399 get paid $9.25 for the first 4 hours and $10.50 for the rest.
Locations 400-499 get paid $13.50 per hour on Sundays (day 1) and Saturdays (day 7) and $6.75 per hour
otherwise.
Locations 500-599 get paid $8.00 per hour for the first 6 hours per day and $12.00 per hour after that.

I don't really know where to start with it I have some what of a start for location 100-199
but its not complete I was wondering if any one can help me:
1
2
3
4
// if else statement for the locations
	if(location >= 100 && location <= 199)

	else

that's my code so far but I don't really know where to go from this
if anyone can help me.
I think you are doing ok:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
if(location >= 100 && location <= 199) {
    ...your calculations here
}
else if (location >= 200 && location <= 299) {
    ...your calculations here
}
else if (location >= 300 && location <= 399) {
    ...your calculations here
}
else if (location >= 400 && location <= 499) {
    ...your calculations here
}
else if (location >= 500 && location <= 599) {
    ...your calculations here
}
else {
   ... your calculations here
}
Last edited on
I'd do this as a function that returns the total pay. The parameters are the location, number of hours worked and day of the week.
1
2
double getPay(int location, double hoursWorked, int dayOfWeek)
{ ... }

Inside is an if/then/else ladder for the different locations:
1
2
3
4
5
6
7
if (location >= 100 && location <= 199) {
    // compute $10/hr with time and half for any hours over 5
    // return the result
} else if (location > 200 && location <= 299) {
    // compute $7.50/hr with double time for hours over 6
    // result the result.
} else ....

Now, I've purposely put a hard-to-catch bug in this code. Do you see it? This works for every location from 100 to 299 except 200. The comparison at line 4 should be <= instead of <. To avoid errors like this, it's good to do ladder comparisons like this instead:
1
2
3
4
5
6
7
8
9
10
11
12
if (location < 100 || location > 599) {
    cerr << location << " is an invalid location\n";
    return -1;
}
// From here on you know that location is valid.
if (location < 200) {
    // compute $10/hr with time and half for any hours over 5
    // return the result
} else if (location < 300) {
    // compute $7.50/hr with double time for hours over 6
    // result the result.
} else ....

By doing it this way, you're guaranteed that you won't leave anything out. You could still get the range wrong, but it's a little harder.
thanks for helping but Im having one problem how would you do the calculations
I think you should understand what is asked to calculate. We can give the solutions on how to calculate but it is spoon feeding. You should at least try to code the calculations based on how you understand the problem and ask for help if you are stuck.
Topic archived. No new replies allowed.