/*
ACTION 7-1 (ACT07_01.CPP)
Output: A program menu that prompts the user to select a
monthly payment, total interest, or total loan amount
calculation option.
Invalid entry messages as required.
The monthly loan payment, total loan interest,
or total loan amount, depending on the program option that the
user selects.
Invalid entry messages as required.
Input: A user response to the menu (P, I, T, or Q).
If P is selected: User enters the loan amount, interest rate, and term.
If I is selected: User enters the loan amount, interest rate, and term.
If R is selected: User enters the loan amount, interest rate, and term.
If Q is selected: Terminate program.
Processing: Calculate the selected option as follows:
Case V: payment = principle * rate/(1 - (1+rate)-term)
Case I: interest = term * payment - principle
Case T: total = principle + interest
Case Q: Quit the program.
where: principle is the amount of the loan.
rate is a monthly interest rate in decimal form.
term is the number of months of the loan.
*/
//PREPROCESSOR DIRECTIVES
#include //FOR cin AND cout
#include //FOR pow()
//MAIN FUNCTION
int main()
{
//DEFINE AND INITIALIZE VARIABLES
char choice = 'Q'; //USER MENU ENTRY
double payment = 0.0; //MONTHLY PAYMENT
double interest = 0.0; //TOTAL INTEREST FOR LIFE OF LOAN
double total = 0.0; //TOTAL LOAN AMOUNT = PRINCIPLE + INTEREST
double principle = 0.0; //LOAN AMOUNT
double rate = 0.0; //INTEREST RATE
int term = 0; //TERM OF LOAN IN MONTHS
//SET OUTPUT FORMAT
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
//DISPLAY PROGRAM DESCRIPTION MESSAGE
cout << "This program will calculate a monthly loan\n"
<< "payment, total loan interest, or total loan amount." << endl;
//MENU LOOP - DO UNTIL ENTRY 'q' OR 'Q'
do
{
//displayMenu() FUNCTION
cout << "\n\n\t\t\tEnter P to get monthly payment"
<< "\n\t\t\tEnter I to get total loan interest"
<< "\n\t\t\tEnter T to get total loan amount"
<< "\n\t\t\tEnter Q to quit" << endl;
cout << "\n\n\tPlease enter your choice: ";
//READ USER CHOICE
cin >> choice;
//loanCalculator() FUNCTION
switch (choice)
{
case 'p': //calculatePayment() FUNCTION
case 'P' :cout << "\nEnter the amount of the loan: $";
cin >> principle;
cout << "\nEnter the duration of the loan in months: ";
cin >> term;
cout << "\nEnter the annual interest rate: ";
cin >> rate;
//CHECK FOR INVALID RATE
while ((rate <= 0) || (rate > 100))
{
//DISPLAY INVALID ENTRY MESSAGE
cout << "\n\nThis is an invalid entry." << endl;
cout << "\nEnter the annual interest rate: ";
cin >> rate;
}//END WHILE
//CALCULATE AND DISPLAY PAYMENT
rate = rate/12/100;
payment = principle * rate/(1 - pow((1+rate), -term));
cout << "\n\nThe monthly payment is $" << payment << endl;
break;
case 'i': //calculateInterest() FUNCTION
case 'I' : cout << "\nEnter the amount of the loan: $";
cin >> principle;
cout << "\nEnter the duration of the loan in months: ";
cin >> term;
cout << "\nEnter the annual interest rate: ";
cin >> rate;
//CHECK FOR INVALID RATE
while ((rate <= 0) || (rate > 100))
{
//DISPLAY INVALID ENTRY MESSAGE
cout << "\n\nThis is an invalid entry." << endl;
cout << "\nEnter the annual interest rate: ";
cin >> rate;
}//END WHILE
//CALCULATE AND DISPLAY INTEREST
rate = rate/12/100;
payment = principle * rate/(1-pow((1+rate), -term));
interest = term * payment - principle;
cout << "\n\nThe total interest is $" << interest << endl;
break;
case 't': //calculateTotalLoanAmount() FUNCTION
case 'T': cout << "\nEnter the amount of the loan: $";
cin >> principle;
cout << "\nEnter the duration of the loan in months: ";
cin >> term;
cout << "\nEnter the annual interest rate: ";
cin >> rate;
//CHECK FOR INVALID RATE
while ((rate <= 0) || (rate > 100))
{
//DISPLAY INVALID ENTRY MESSAGE
cout << "\n\nThis is an invalid entry." << endl;
cout << "\nEnter the annual interest rate: ";
cin >> rate;
}//END WHILE
//CALCULATE AND DISPLAY TOTAL
rate = rate/12/100;
payment = principle * rate/(1-pow((1+rate), -term));
interest = term * payment - principle;
total = principle + interest;
cout << "\n\nThe total loan amount is $" << total << endl;
break;
case 'q': //QUIT THE PROGRAM
case 'Q': cout << "Program terminated" << endl;
break;
//DISPLAY INVALID ENTRY MESSAGE
default : cout << "\n\nThis is an invalid entry. Please"
<< " select again." << endl;
} //END SWITCH
}//END WHILE
while ((choice != 'q') && (choice != 'Q'));
//RETURN
return 0;
} //END main()