/* ACTION 8-1 (ACT08_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 #include //FUNCTION PROTOTYPES char displayMenu(); void loanCalculator(char choice); double calculatePayment(); double calculateInterest(); double calculateTotalLoanAmount(); //MAIN FUNCTION int main() { //DEFINE VARIABLES char choice = 'Q'; //USER MENU ENTRY //DISPLAY PROGRAM DESCRIPTION MESSAGE cout << "\nThis program will calculate a monthly loan interest\n" << "payment, total loan interest, or total loan amount." << endl; //MENU CONTROL LOOP do { choice = displayMenu(); loanCalculator(choice); } //END DO/WHILE while ((choice != 'q') && (choice != 'Q')); //RETURN return 0; }//END main() //DISPLAY MENU FUNCTION char displayMenu() { //DEFINE LOCAL CHOICE VARIABLE char choice = 'Q'; 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; //RETURN CHOICE return choice; } //END displayMenu() //LOAN CALCULATION FUNCTION void loanCalculator(char choice) { switch (choice) { case 'p': //CALL calculatePayment() FUNCTION case 'P': cout << "The monthly loan payment is $" << calculatePayment() << endl; break; case 'i': //CALL calculateInterest() FUNCTION case 'I' : cout << "The monthly loan payment is $" << calculateInterest() << endl; break; case 't': //CALL calculateTotalLoanAmount() FUNCTION case 'T': cout << "The monthly loan payment is $" << calculateTotalLoanAmount() << endl; break; case 'q': //TERMINATE PROGRAM case 'Q': cout << "Program terminated." << endl; break; //DISPLAY INVALID ENTRY MESSAGE default : cout << "\n\nThis is an invalid entry." << endl; } //END SWITCH }//END loanCalculator() //CALCULATE PAYMENT FUNCTION double calculatePayment() { //DEFINE LOCAL VARIABLES double principle = 0.0; //LOAN PRINCIPLE double rate = 0.0; //ANNUAL INTEREST RATE int term = 0; //TERM OF LOAN IN MONTHS //GET LOAN DATA FROM USER 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 PAYMENT rate = rate/12/100; return principle * rate/(1-pow((1+rate),-term)); }//END calculatePayment() //CALCULATE INTEREST FUNCTION double calculateInterest() { //DEFINE LOCAL VARIABLES double payment = 0.0; //MONTHLY PAYMENT double principle = 0.0; //LOAN PRINCIPLE double rate = 0.0; //ANNUAL INTEREST RATE int term = 0; //TERM OF LOAN IN MONTHS //GET LOAN DATA FROM USER 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 INTEREST rate = rate/12/100; payment = principle * rate/(1-pow((1+rate),-term)); return term * payment - principle; }//END calculateInterest() //CALCULATE TOTAL LOAN AMOUNT FUNCTION double calculateTotalLoanAmount() { //DEFINE LOCAL VARIABLES double payment = 0.0; //MONTHLY PAYMENT double interest = 0.0; //TOTAL INTEREST FOR LIFE OF LOAN double principle = 0.0; //LOAN PRINCIPLE double rate = 0.0; //ANNUAL INTEREST RATE int term = 0; //TERM OF LOAN IN MONTHS //GET LOAN DATA FROM USER 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 TOTAL LOAN AMOUNT rate = rate/12/100; payment = principle * rate/(1-pow((1+rate),-term)); interest = term * payment - principle; return principle + interest; }//END calculateTotalLoanAmount()