having a real tough time with functions..
I'm not sure why but some of the variable don't seem to pick up values. I'm having a hard time with functions, can't seem to grasp how pass values back and forth from main to the functions. sorry for the mess of a code and thanks for any help.
Code:
#include <iostream>
#include <iomanip>
using namespace std;
// function one, getting input
void input(double &, double &, double &);
// function two, calculate interest rate and exits if the payment isn't enough
void interestRate (double &, double &, double &, double &, double &);
// function three, the main working function, calculates the total interest, the final payments and the amount of months it takes.
void calculations (int &, double &, double &, double &, double &, double &, double &);
// function four, display the results
void results (int &, double &, double &);
int main() // all main does is call on the functions
{
double money, interest, desiredPayment, realRate, totalInterest, finalPayment, monthlyInterest, minPay;
int months;
input(money, interest, desiredPayment);
interestRate(realRate, money, interest, desiredPayment, minPay);
calculations(months, monthlyInterest, totalInterest, money, realRate, finalPayment, desiredPayment);
results(months, totalInterest, finalPayment);
return 0;
}
// this function gets the input from the user and puts it into reference variables.
void input (double &money, double &interest, double &desiredPayment)
{
cout << "How much do you want to borrow? $" ;
cin >> money ;
while (money <= 0)
{
cout << "You must enter a positive number!\n";
cout << "How much do you want to borrow? $";
cin >> money ;
}
cout << "What is the annual interest rate expressed as a percent? ";
cin >> interest;
while (interest <= 0)
{
cout << "You must enter a positive number!\n";
cout << "What is the annual interest rate expressed as a percent? ";
cin >> interest;
}
cout << "What is the monthly payment amount? $";
cin >> desiredPayment;
while (desiredPayment <= 0)
{
cout << "You must enter a positive number!\n";
cout << "What is the monthly payment amount? $";
cin >> desiredPayment;
}
}
// this function calculates the interest rate, and notifies if the minimum monthly payment isn't sufficient.
void interestRate (double &desiredPayment, double &money, double &interest, double &realRate, double &minPay )
{
realRate = money *(interest/1200.0); // for some reasong realRate stays 0
if (desiredPayment <= realRate)
{
minPay = realRate + 1 ;
cout << fixed << showpoint << setprecision(2);
cout << "You must make payments of at least $" << minPay << endl;
cout << "Because your monthly interest is $" << realRate;
cout << endl;
cout << endl;
cout << "** Don't get overwhelmed with debt! **\n\n";
exit(0);
}
else
realRate = money *(interest/1200.0);
}
// This is the workhorse function, it calculates how many months it will take to pay off the loan,
// what the final payment will be,
// how much interest was payed over time,
void calculations (int &months, double &monthlyInterest, double &totalInterest, double &money, double &realRate, double &finalPayment, double &desiredPayment)
{
months = 1 ;
while (desiredPayment <= money)
{
monthlyInterest = money * realRate ;
totalInterest = monthlyInterest + totalInterest ;
money = money - desiredPayment ;
months ++ ;
}
money = finalPayment ;
}
// This function displays the results
void results (int &months, double &totalInterest, double &finalPayment)
{
cout << fixed << showpoint << setprecision(2);
cout << "Your debt will be paid off after "<< months << "months, with a final payment of just " << finalPayment ;
cout << " The total amount of interest you will pay during that time is " << totalInterest << "\n\n";
cout << "** Don't get overwhelmed with debt! **\n\n";
}