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
- Create
bonus. Declare anintbelow the starter data and set it tosalary * 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. - Print the name. Send
nametocoutand finish withendl, which ends the line and flushes the output. Without it, the next print would continue on the same line. - Print the bonus line. Chain the text
"Bonus: $"and the number with<<. Unlike string concatenation with+, the<<operator prints anintdirectly, 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 << bworks because each<<returns the stream, so the next one continues where the previous stopped. Each piece can have its own type: a string, then anint.endlwrites 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
productivityvariable is harmless. The program compiles and runs the same with or without it.
Edge cases and common mistakes
- Dividing first.
salary / 100 * 15loses the remainder of the first division. Always multiply before dividing in integer percentages. - Using
0.15.salary * 0.15is adouble, and storing it in aninttruncates 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.
intis usually 32 bits, sosalary * 15overflows 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 aslong longis 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:
| salary | Output after the name |
|---|---|
| 135000 | Bonus: $20250 |
| 99 | Bonus: $14 |
| 6 | Bonus: $0 |
A bonus of 0 for a salary of 6 is correct: 15% of 6 is 0.9, which rounds down to 0.
