Another C++ Program and Anniversary Day!!!

May 07, 2011 21:23

31 months is a long time. :) 4 months and 3 days from now we will be married and partying our tushies off. As for tonight, both of us are working on school.

C++ is going well. First real test is online and due by tomorrow. I just finished my latest program. The whole purpose for this program is to show that we can use functions, and it would be a lot shorter if we didn't have explicit instructions to use functions. :/

Please feel free to check it out and give feedback if you are so inclined...


/***********************************************************************
Description: This program shows how to use functions
***********************************************************************/

#include
#include
//#include

using namespace std;

// Function declarations
double findInt(double interestRate, int yearPayments);
double loanAmaountCalc(double i, double exp, double periodicPayment);
double monthlyPaymentCalc(double i, double exp, double periodicPayment);

void main()
{
double salary;
double interestRate;
int loanLength = -30;
int yearPayments = 12;
double periodicPayment = 0;

// Get gross salary and interest rate from user
cout << "Please enter your yearly gross salary: ";
cin >> salary;
cout << "Please enter an interest rate: ";
cin >> interestRate;

// Convert interest rate to decimal
interestRate = interestRate * 0.01;

// Determine periodicPayment
periodicPayment = ((salary / 12) * .3);

// Calculate "exp" for later use
double exp = -(yearPayments * loanLength);

// Call function to calculate "i" for later use
double i = findInt(interestRate, yearPayments);

// Call function to calculate the monthly payment
double monthlypayment = monthlyPaymentCalc(i, exp, periodicPayment);

// Call function to calculate the loan amount
double loanAmount = loanAmaountCalc(i, exp, periodicPayment);

// Set precision to two decimals
cout.precision(2);
cout << fixed << showpoint;

// Output to screen desired results
cout << "The target (30% of monthly salary) monthly payment range is: $" << (periodicPayment - 10)
<< " - \n$" << periodicPayment << endl;
cout << "Your max target house price is: $" << loanAmount << endl;
cout << "with a monthly payment of: $" << monthlypayment << endl;

system("pause");
}

// Function for calculating "i"
double findInt(double interestRate, int yearPayments)
{
double i = 0;
i = interestRate / yearPayments;
return i;
}

// Function for calculating loanAmount
double loanAmaountCalc(double i, double exp, double periodicPayment)
{
double loanAmount = 0;
double monthlyPayment = 0;
while (periodicPayment > monthlyPayment && monthlyPayment < (periodicPayment - 10))
{
loanAmount += 1000;
monthlyPayment = (loanAmount * i / (1 - pow((1 + i),-(exp))));
}
return loanAmount;
}

// Function for finding monthlyPayment
double monthlyPaymentCalc(double i, double exp, double periodicPayment)
{
double loanAmount = 0;
double monthlyPayment = 0;
while (periodicPayment > monthlyPayment && monthlyPayment < (periodicPayment - 10))
{
loanAmount += 1000;
monthlyPayment = (loanAmount * i / (1 - pow((1 + i),-(exp))));
}
return monthlyPayment;
}

Previous post Next post
Up