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;
Free plan: 30 tutor messages. Premium is unlimited.
Saved
Ctrl + Enter to check
