Problem overview
Two employees are up for the award. The starter data holds name1, productivity1 and salary1 for the first, and the same three variables ending in 2 for the second. The program picks the more productive employee and prints their name and their 15% bonus, rounded down.
If both have the same productivity, employee 1, the one listed first, wins. With the starter data, Mary Jane (productivity 133) beats John Doe (83), and her bonus is 335000 * 15 / 100 = 50250:
Mary Jane
Bonus: $50250
Version 1 computed a bonus for one employee. This version adds the decision of whose salary to use.
Approach: fill two variables in a branch
After the comparison, the rest of the program should not care which employee won. So the plan is to declare two variables, winnerName and winnerSalary, and let an if/else copy the right employee's data into them. The bonus and the printing then use only those two variables, and they are written once.
The tie rule decides the operator. The condition is productivity1 >= productivity2. With > a tie would fall into the else and employee 2 would win, which breaks the rule. With >= a tie keeps employee 1.
Why one if/else and not two separate ifs
It is tempting to write one if for each employee, as in "if employee 1 is at least as productive, take employee 1; if employee 2 is more productive, take employee 2". That can work, but it makes the program test two conditions that are really one question, and it leaves a gap in the reasoning: you have to convince yourself that exactly one of the two conditions is true every time. With if/else that is guaranteed by the language. Either the condition holds or it does not, and the else takes everything else, so exactly one branch runs and both winner variables are always set.
Following the starter data through the code makes this concrete. The condition is 83 >= 133, which is false, so the else branch runs. It copies "Mary Jane" into winnerName and 335000 into winnerSalary. After the branches, bonus is calculated from the copy, and the program prints the name and the bonus.
Step by step
- Declare the winner variables without values: a
string winnerNameand anint winnerSalary. They go before theif, not inside it. A variable declared inside braces exists only until the closing brace, so the lines after theifcould not see it. - Write the
ifwith the conditionproductivity1 >= productivity2, and close its brace right away so the structure is complete. The next steps fill in the body. - Store employee 1's name in
winnerName. - Store employee 1's salary in
winnerSalary. The name and salary are always set as a pair, so the winner's data stays consistent. - Add the
else. It continues from the}that ends theifbody, in the form} else {. It runs only when employee 2 is strictly more productive. - Store employee 2's name in
winnerName. - Store employee 2's salary in
winnerSalary. - Compute
bonusbelow the if/else aswinnerSalary * 15 / 100. Multiplying first and dividing by 100 afterwards gives a whole number rounded down, because dividing twointvalues drops the fraction. - Print the name of the winner, ending with
endl. - Print the bonus line by chaining
"Bonus: $"andbonuswith<<.
The solution in C++
string name1 = "John Doe";
int productivity1 = 83;
int salary1 = 135000;
string name2 = "Mary Jane";
int productivity2 = 133;
int salary2 = 335000;
string winnerName;
int winnerSalary;
if (productivity1 >= productivity2) {
winnerName = name1;
winnerSalary = salary1;
} else {
winnerName = name2;
winnerSalary = salary2;
}
int bonus = winnerSalary * 15 / 100;
cout << winnerName << endl;
cout << "Bonus: $" << bonus << endl;
Details worth a closer look:
- Variables with no initial value.
string winnerName;starts as an empty string.int winnerSalary;starts with an indeterminate value, and reading it before assigning is undefined behaviour. This is safe here because both branches of theif/elseassign it, so every path sets it before it is used. - Scope. The assignments inside the branches change the outer variables. Writing
string winnerName = name1;inside a branch would instead create a new variable that disappears at the closing brace, and the outer one would stay unset. - Copying strings.
winnerName = name1;copies the text. Changingname1later would not affectwinnerName.
Edge cases and common mistakes
- Ties. Two employees with productivity 100 and salaries 200000 and 400000 give a bonus of 30000, which is employee 1's. Using
>instead of>=would give 60000, employee 2's. - Declaring inside the branch. The most common error in this lesson is declaring
winnerNamein a branch and then finding it unavailable at the print. - Mixing up the pair. If a branch sets the name from one employee and the salary from the other, the output is a wrong combination such as the winner's name with the loser's bonus. Always set both from the same employee.
- Comparing the wrong values. The condition compares productivity, not salary.
Testing
Running the solution's logic with other data gives these results:
| Employee 1 (name, productivity, salary) | Employee 2 | Output |
|---|---|---|
| John Doe, 83, 135000 | Mary Jane, 133, 335000 | Mary Jane, then Bonus: $50250 |
| A, 100, 200000 | B, 100, 400000 | A, then Bonus: $30000 |
| A, 120, 200000 | B, 100, 400000 | A, then Bonus: $30000 |
