Compound Interest

Hi, I am making a program that calculates compounded monthly interest. I ask for the initialbalance and the number of months that it will be compounded and ask to output the final balance after those months. For some reason when I run the program the final balance is always zero. I am not sure why it is doing that but I tried debugging but it still does not make sense for me on how the balance = 0 in the Compounded function (line 34). Can someone help me identify the problem here, I am just utterly confused.

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
#include <string>
#include <iostream>

using namespace std; 

void GetParam(double & initialbalance, double &interestrate, double &compmonth)
{
	cout << "Please enter the initial balance: "; 
	cin >> initialbalance;
	if(initialbalance >= 0 && initialbalance <= 99.99)
	{
		interestrate =(3.0/100);
	}
	else if(initialbalance >= 100.00 && initialbalance <= 999.99)
	{
		interestrate = (3.5/100); 
	}
	else 
	{
		interestrate = (4.0/100); 
	}
	cout << "Please enter number of months compounded: " ; 
	cin >> compmonth; 
}

double Compounded(double initialbalance, double interestrate, double compmonth)
{
	double balance, interest;
	double month = 0; 
	balance = initialbalance;
	while(month <= compmonth)
	{
		interest = balance*(1/12)*interestrate;
		balance = interest + balance;
		month++;
	}
	return balance; 
}
int main()
{
	double initialbalance, interestrate, compmonth; 
	double newbalance; 
	GetParam(initialbalance,interestrate,compmonth);
	newbalance = Compounded(initialbalance,interestrate,compmonth);
	cout << newbalance << endl;


	return 0; 
}
Last edited on
1/12 is an integer expression, and so uses integer division. 1/12 == 0. Use 1.0/12.0 to use floating point division.
Topic archived. No new replies allowed.