Start learning free

LESSON 53 OF 281 · CALCULATIONS - CHALLENGES

Employee Of The Year V1 in C++: One Employee and a Bonus

Practice Read the full explanation

Compute an end-of-year performance bonus for one employee, then print the employee's name and the bonus.

You start with:

string name = "John Doe";
int productivity = 83;
int salary = 135000;

Build:

John Doe
Bonus: $20250

Rules:

  • The bonus is 15% of the salary, rounded down to a whole number.
  • Print the employee's name on the first line.
  • Print the bonus on the second line as "Bonus: $<amount>".
  • productivity is not used in this version. It is there for the next versions, where you will compare employees by productivity.

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

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

string name = "John Doe";
int productivity = 83;
int salary = 135000;

int bonus = salary * 15 / 100;
cout << name << endl;
cout << "Bonus: $" << bonus << endl;

STEP 1 OF 3
    Ctrl + Enter to check

    C++ · CALCULATIONS - CHALLENGES

    Employee Of The Year V1 in C++, explained

    Problem overview

    This first version of the Employee of the Year project works with a single employee. The starter code gives you three variables: name, productivity and salary. The program has to compute a bonus equal to 15% of the salary, rounded down to a whole number, and print two lines: the employee's name, then Bonus: $ followed by the amount.

    With salary = 135000, 15% is 20250, so the program prints:

    John Doe
    Bonus: $20250
    

    The variable productivity is not used here. It is part of the starter data because later versions of the project compare employees by it; in this one you can leave it untouched.

    Approach: integer arithmetic does the rounding

    A percentage is a multiplication followed by a division. The natural way to write 15% is salary * 0.15, but that produces a floating-point number, and you would then have to convert it back to a whole number. There is a simpler route that stays with int the whole time: multiply by 15 first, then divide by 100.

    In C++, dividing two int values gives an int. The fractional part is discarded, so the result is rounded down for positive numbers. That is exactly the rounding the rules ask for, and no extra code is needed.

    The order matters. salary * 15 / 100 multiplies first, so no precision is lost before the division. If you wrote salary / 100 * 15, the division would drop the cents of the salary first, and the bonus could come out smaller than it should. With a salary of 99, the correct bonus is 14 (14.85 rounded down), while 99 / 100 * 15 is 0.

    Step by step

    1. Create bonus. Declare an int below the starter data and set it to salary * 15 / 100. Reading left to right, C++ evaluates the multiplication first and then the division, because * and / have the same precedence and group from the left.
    2. Print the name. Send name to cout and finish with endl, which ends the line and flushes the output. Without it, the next print would continue on the same line.
    3. Print the bonus line. Chain the text "Bonus: $" and the number with <<. Unlike string concatenation with +, the << operator prints an int directly, so no conversion is needed. The dollar sign is an ordinary character inside the quotes.

    The solution in C++

    string name = "John Doe";
    int productivity = 83;
    int salary = 135000;
    
    int bonus = salary * 15 / 100;
    cout << name << endl;
    cout << "Bonus: $" << bonus << endl;
    

    A few details worth knowing:

    • cout << a << b works because each << returns the stream, so the next one continues where the previous stopped. Each piece can have its own type: a string, then an int.
    • endl writes a newline and flushes. "\n" writes only the newline. For a program this small the difference does not show.
    • Integer division by a positive number rounds toward zero. With positive salaries that means rounding down. A negative salary would round toward zero instead, which is not a case this lesson has.
    • The unused productivity variable is harmless. The program compiles and runs the same with or without it.

    Edge cases and common mistakes

    • Dividing first. salary / 100 * 15 loses the remainder of the first division. Always multiply before dividing in integer percentages.
    • Using 0.15. salary * 0.15 is a double, and storing it in an int truncates the fraction. That works, but it brings floating-point numbers into a calculation that does not need them. The integer form states the rule exactly.
    • Overflow. int is usually 32 bits, so salary * 15 overflows for salaries above about 143 million. Signed overflow is undefined behaviour in C++. The lesson's salaries are far below that, but a larger type such as long long is the safe choice if the numbers can grow.
    • Forgetting endl. The name and the bonus line then run together on one line.

    Testing

    Running the same calculation with other salaries gives these results:

    salaryOutput after the name
    135000Bonus: $20250
    99Bonus: $14
    6Bonus: $0

    A bonus of 0 for a salary of 6 is correct: 15% of 6 is 0.9, which rounds down to 0.

    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