This program is supposed to take the four inputs and use them to calculate the balance for each year of the users N amount of years.
Balance = P * (1 + I / Q ) ^ (N * Q)
I've managed to do pretty much everything except get it to output the answers. I keep getting jumbled output. Any help would be greatly appreciated. Thanks.
Code:
#include <fstream>
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
void screenHeader(); // Parameterless Void Function
void inputValues(float &p, float &i, float &q, int &n); // Passed by reference
float getBalance(float p, float i, float q, int n); // Passed by Value
int main()
{
int another = 1;
float p, i, q;
int n; // Declaring Variables
char answer;
float balance;
system("clear"); // Clears Screen
screenHeader(); // Calling Void
while(another != 0) { // While Loop
system("clear");
screenHeader();
inputValues(p, i, q, n);
cout << "--------------------------------------------" << endl;
cout << "Principal amount======================> " << p << endl;
cout << "Annual interest rate==================> " << i << endl;
cout << "Times compounded per year=============> " << q << endl;
cout << "Years to keep in account==============> " << n << endl;
for(int count = 1; count <= n; count++) // For Loop
{
balance = getBalance(p, i, q, count);
cout << "--------------------------------------------" << endl;
cout << "Balance after " << count << " years: " << balance << endl;
}
cout << "--------------------------------------------" << endl;
cout << "--------------------------------------------" << endl;
cout << "Would you like to do another calculation? Enter Y or N: ";
cin >> answer;
if(answer != 'y' && answer != 'Y')
another = 0;
} // end of while loop
return 0;
}
void screenHeader()
{
cout << "Program SAVE" << endl;
cout << "--------------------------------------------" << endl;
cout << "This program will request that you enter" << endl;
cout << "values for: " << endl;
cout << " " << endl;
cout << " a principal amount (P) " << endl;
cout << " an annual interest rate (I) " << endl; // Cout's
cout << " times compounded per year (Q) " << endl;
cout << " years in account (N) " << endl;
cout << " " << endl;
cout << "The program will then calculate and print " << endl;
cout << "the balance for each year up through year N." << endl;
cout << "--------------------------------------------" << endl;
cout << " " << endl;
}
void inputValues(float &p, float &i, float &q, int &n)
{
cout << "Please enter your principal amount: " << endl; // Inputting Values and Outputting Statements
cin >> p;
cout << "Please enter the annual interest rate: " << endl;
cin >> i;
cout << "Please enter the times compounded per year: " << endl;
cin >> q;
cout << "Please enter the number of years in the account: " << endl;
cin >> n;
}
float getBalance(float p, float i, float q, int n)
{
return (pow (( p*(1+ (i / q))),(n * q)));
}