setprecision not working

This program has 10 different salespeople, and their total sales. The user puts in a bonus rate, and the program calculates and displays the bonus for each individual, and then the total bonus of all the sales. I need to display all the bonus values with 2 decimal places. I desk check the program using .10 as the bonus rate, and the bonuses won't show up with two decimal places. (Code on line 29 and 33).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
  //Advanced25.cpp - Calculates and displays the total bonus, displays the total sales, and the corresponding salesperson.
//Created/revised by T.C. Bascom on May 17, 2015

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
	//Declare arrays/variables
	int salesPerPerson[10][2] = { { 1, 7900 }, { 2, 9500 }, { 3, 3150 }, { 4, 1530 }, { 5, 3000 }, { 6, 21300 }, { 7, 2450 }, { 8, 14200 }, { 9, 14400 }, { 10, 2900 } };
	double bonusRate = 0.0;
	int bonus = 0;
	int rows = 0;
	int totalBonus = 0;

	//Get bonus rate
	cout << "Bonus rate as a decimal: ";
	cin >> bonusRate;

	//Clear screen to look more professional
	system("cls");
	
	//Calculate total bonus. Run loop to display total sales and total bonus for corresponding salesperson.
	while (rows < 10)
	{
		bonus = salesPerPerson[rows][1] * bonusRate;
		totalBonus = totalBonus + bonus;
		cout << "Salesperson " << salesPerPerson[rows][0] << " Sales: " << salesPerPerson[rows][1] << " Bonus: $" << fixed << setprecision(2) << bonus << "\n";
		rows += 1;
	}
	//Display bonus for all sales
	cout << "Total bonus for all sales: $" << fixed << setprecision(2) << totalBonus << endl;

	system("pause");
	return 0;
}	//end of main function 
Last edited on
Had to change the variables bonus and totalBonus to doubles, then it worked fine.
Topic archived. No new replies allowed.