I made this program in C++, simple calculation of an interest rate of a bank a while back as part of a homework assignment. The answer is incorrect by a small margin but I still cannot understand why, and the mistake gets higher as I try higher input numbers... The instructions on how to get this problem are commented as first lines of the program.
I tried switching the involved variables from float to double then to long double and its still the same answer...
Can anyone please figure out why?
// Homework 2 Task 1.cpp : Show bank balance after loan with user-input factors
//Try the code with 100 deposited sum, 5% interest and 3 months total time
//The answer will show 302.087 whereas the true answer should be 302.507
#include "stdafx.h"
#include <iostream>
using namespace std;
long double compoundVal(unsigned int, unsigned short int, unsigned short int);
void main()
{
unsigned int DepSum;
unsigned short int IntRate, NrMonths;
cout << "Please enther the amount you expect to deposit each month: ";
cin >> DepSum;
cout << "\nThe amount of money that you will have in your account after 6 months with Inte-rest Rate of 5% will be: "<<compoundVal(DepSum, 5, 6); //Answering the first part of this task, where the user has to specify the Deposit Sum, and will receive the amount after 6 months with interest of 5%
cout << "\n\nYou can also see the account balance with interest rate and number of months of your choice.\nPlease enter the Interest Rate of your choice: ";
cin >> IntRate;
cout << "\nNow enter the number of months you intend to have the account: ";
cin >> NrMonths;
cout << "\nThis will be your account balance: " << compoundVal(DepSum, IntRate, NrMonths) << endl;
}
long double compoundVal(unsigned int d, unsigned short int i, unsigned short int n){
long double c = (1.0 + (i / 1200.0)); //Finding the monthly percentage, and because the user inputs the yearly interest in %, we need to remove the %(*0.01) and /12 for 12 months/year.
return((1.0 + (n - 1)*c)*d*c); //The math formula that will give us the results of the calculation.
}