Setprecision use

How do format my output to 2 decimal places using setprecision

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  //Credit Card Interest Program

#include <iostream>
#include <iomanip> 

using namespace std;

int main()
{
	double netBalance;							//The balance shown in the bill.
	double payment;								//The payment made.
	int d1;										//The number of days of the billing cycle.
	int d2;										//The number of days payment is made before billing cycle.
	double averageDailyBalance;					//The average daily balance
	double interest;							//The interest on the unpaid balance
	double interestRate;						//The interest rate per month

	cout << fixed << showpoint;

	cout << "Enter the net balance: ";			
	cin >> netBalance;						
	cout << endl;								

	cout << "Enter the payment made: ";
	cin >> payment;
	cout << endl;

	cout << "Enter the number of days of the billing cycle: ";
	cin >> d1;
	cout << endl;

	cout << "Enter the number of days payment is made before billing cycle: ";
	cin >> d2;
	cout << endl;

	cout << "Enter interest rate per month: ";
	cin >> interestRate;
	cout << endl;
	
	cout << "The average daily balance is: " << (netBalance * d1 - payment * d2) / d1 << endl;
	cin >> averageDailyBalance;
	cout << endl;

	cout << "The interest on the unpaid balance: " << averageDailyBalance * 0.0152 << endl;
	cin >> interest;
	cout << endl;


	return 0;

}
I'm also not getting any output for this.

1
2
3
cout << "The interest on the unpaid balance: " << averageDailyBalance * 0.0152 << endl;
	cin >> interest;
	cout << endl;


What am I missing?
You just need to do this
 
    cout << fixed << setprecision(2);

Other than that, the end part of your code makes no sense at all. I have no idea what you are trying to achieve with the cin statements, so I can't help you with that.
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <iomanip> 

using namespace std;

int main()
{
	double netBalance;						//The balance shown in the bill.
	double payment;							//The payment made.
	int d1;									//The number of days of the billing cycle.
	int d2;									//The number of days payment is made before billing cycle.
	double averageDailyBalance;				//The average daily balance
	double interest;						//The interest on the unpaid balance
	double interestRate;					//The interest rate per month

	cout << fixed << showpoint;

	cout << "Enter the net balance: ";		//output
	cin >> netBalance;						//input
	cout << endl;							//output	

	cout << "Enter the payment made: ";		//output
	cin >> payment;							//input
	cout << endl;							//output

	cout << "Enter the number of days of the billing cycle: ";		//output
	cin >> d1;														//input
	cout << endl;													//output

	cout << "Enter the number of days payment is made before billing cycle: ";		//output
	cin >> d2;																		//input
	cout << endl;																	//output

	cout << "Enter interest rate per month: ";		//output
	cin >> interestRate;							//input
	cout << endl;									//output
	
	averageDailyBalance = (netBalance * d1 - payment * d2) / d1;		//The average daily balance
	cout << fixed << setprecision(2);									//setting the decimals
	cout << "The average daily balance is: " << averageDailyBalance;
	cout << endl;

	cout << endl; //provide seperation between lines
	interest = averageDailyBalance * interestRate; //
	cout << fixed << setprecision(2);									//setting the decimals
	cout << "The interest on the unpaid balance: " << interest;
	cout << endl;

	cin.get();		//program was closing too fast; this slows it down
	cin.get();


	return 0;

}


I fix it. Thanks for the help.
tpb is correct on the precision.

I'm assuming that the lines
1
2
3
4
5
6
7
cout << "The average daily balance is: " << (netBalance * d1 - payment * d2) / d1 << endl;
cin >> averageDailyBalance;
cout << endl;

cout << "The interest on the unpaid balance: " << averageDailyBalance * 0.0152 << endl;
cin >> interest;
cout <<endl;

are trying to take the output of cout and place it in cin which is not how cin works by default.

instead:
1
2
3
4
5
averageDailyBalance = (netBalance * d1 - payment * d2) / d1;
cout << "the ave ... balance is: " << averageDailyBalance << endl;

interest = averageDailyBalance * 0.0152 << endl;
cout << "The inter....unpaid balance: " << interest << endl;


Just a side note on fixed and setprecision. They will stay active until you modify cout with different manipulators, so while its fine for clarity to use multiple
 
cout << fixed << setprecision(2);

before each output, it's not necessary. Also, fixed mode will stay in effect until the end of the program, so if after several statements you need to remove the fixed modifier for some reason, use
 
cout.unsetf(ios::fixed)
Last edited on
Topic archived. No new replies allowed.