Help with dividing with doubles

Hi, I'm writing a program that displays the balance of a loan after the first three monthly payments given the monthly payment, annual interest rate, and number of payments. I'm running into a problem calculating the interest rate per month. For example when I enter 0.09 for the annual interest rate my program prints 0.01 instead of 0.0075. The rest of my program seems to be working fine when printing out the balance after the first three payments. The function to calculate the interest rate per month is i = (rate / 12.0); which should return a double. Any help at all would be great. Thanks.

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
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
   double payment, rate, i, x, y, z, one, two, three;
   int months;
       
   cout << "Enter payment amount: " << flush;
   cin >> payment;
   
   cout << "Enter interest rate (9% as 0.09): " << flush;
   cin >> rate;
   
   cout << "Enter number of payments: " << flush;
   cin >> months;
   
   cout << " " << endl;
   
   cout << showpoint << fixed;
   
   //power functions for the first three months
   x = pow((1+rate/12.0), (1-months));
   y = pow((1+rate/12.0), (2-months));
   z = pow((1+rate/12.0), (3-months));
   
   // interest rate per month
   i = (rate / 12.0);
   
   // balance after first month       
   one = payment * ((1-x) / (rate/12.0));
   //balance after second month 
   two = payment * ((1-y) / (rate/12.0));
   //balance after third month
   three = payment * ((1-z) / (rate/12.0));
         
   cout << "Payment entered was: $" << setprecision(2) << payment << endl
        << "Interest rate is " << rate << " which is " << i << " per month" << endl
        << "Number of payments is " << months << endl
        << "Payment #1 leaves a balance of $" << setprecision(2) << one << endl
        << "Payment #2 leaves a balance of $" << setprecision(2) << two << endl
        << "Payment #2 leaves a balance of $" << setprecision(2) << three << endl;
        
   return 0;
}
Last edited on
I found a possible reason for why my program is printing 0.01 instead of 0.0075.

When I deleted

cout << showpoint << fixed;

My program printed 0.0075, but of course my other outputs were shown in scientific notation. So if someone could point me in the direction to print out my outputs not in scientific notation while keeping my "i" value untouched that would be great.

Before:
Enter payment amount: 165.35
Enter interest rate (9% as 0.09): 0.09
Enter number of payments: 36

Payment entered was: $165.35
Interest rate is 0.09 which is 0.01 per month
Number of payments is 36
Payment #1 leaves a balance of $5073.38
Payment #2 leaves a balance of $4946.08
Payment #3 leaves a balance of $4817.82

After:
Enter payment amount: 165.35
Enter interest rate (9% as 0.09): 0.09
Enter number of payments: 36

Payment entered was: $1.7e+02
Interest rate is 0.09 which is 0.0075 per month
Number of payments is 36
Payment #1 leaves a balance of $5.1e+03
Payment #2 leaves a balance of $4.9e+03
Payment #3 leaves a balance of $4.8e+03
Last edited on
You could insert another setprecision() like this:

<< "Interest rate is " << rate << " which is " << setprecision(4) << i << " per month" << endl
Yeah I figured that would be the best option as well. Thank you.
Topic archived. No new replies allowed.