Start learning free

LESSON 95 OF 281 · CONDITIONAL STATEMENTS - CHALLENGES

Employee Of The Year V2 in C++: Choosing a Winner with if/else

Practice Read the full explanation

Two employees are up for the Employee of the Year award. Pick the more productive one and compute their 15% bonus.

You start with:

string name1 = "John Doe";
int productivity1 = 83;
int salary1 = 135000;
string name2 = "Mary Jane";
int productivity2 = 133;
int salary2 = 335000;

Build:

Mary Jane
Bonus: $50250

Rules:

  • The winner is whichever employee has the higher productivity.
  • If both have the same productivity, employee 1 (the one listed first) wins.
  • The winner's bonus is 15% of their salary, rounded down to a whole number.
  • Print the winner's name on the first line, then "Bonus: $<amount>" on the second.

Declare string winnerName; int winnerSalary; before the if, then use if/else to assign them, so both are still in scope for the prints. A tie goes to employee 1, so compare with >=.

Dividing two int values drops the fraction, so winnerSalary * 15 / 100 is already rounded down: int bonus = winnerSalary * 15 / 100;

Chain the text and the number with <<: cout << "Bonus: $" << bonus << endl;

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;

STEP 1 OF 10
    Ctrl + Enter to check

    C++ · CONDITIONAL STATEMENTS - CHALLENGES

    Employee Of The Year V2 in C++, explained

    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

    1. Declare the winner variables without values: a string winnerName and an int winnerSalary. They go before the if, not inside it. A variable declared inside braces exists only until the closing brace, so the lines after the if could not see it.
    2. Write the if with the condition productivity1 >= productivity2, and close its brace right away so the structure is complete. The next steps fill in the body.
    3. Store employee 1's name in winnerName.
    4. Store employee 1's salary in winnerSalary. The name and salary are always set as a pair, so the winner's data stays consistent.
    5. Add the else. It continues from the } that ends the if body, in the form } else {. It runs only when employee 2 is strictly more productive.
    6. Store employee 2's name in winnerName.
    7. Store employee 2's salary in winnerSalary.
    8. Compute bonus below the if/else as winnerSalary * 15 / 100. Multiplying first and dividing by 100 afterwards gives a whole number rounded down, because dividing two int values drops the fraction.
    9. Print the name of the winner, ending with endl.
    10. Print the bonus line by chaining "Bonus: $" and bonus with <<.

    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 the if/else assign 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. Changing name1 later would not affect winnerName.

    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 winnerName in 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 2Output
    John Doe, 83, 135000Mary Jane, 133, 335000Mary Jane, then Bonus: $50250
    A, 100, 200000B, 100, 400000A, then Bonus: $30000
    A, 120, 200000B, 100, 400000A, then Bonus: $30000
    Keep going with C++1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free