33.6k views
1 vote
2) [13 points] You’ve been hired by Banker Bisons to write a C++ console application that determines the number of months to repay a car loan. Write value function getLoanAmount that has no parameters, uses a validation loop to prompt for and get from the user a car loan amount in the range $2,500-7,500, and returns the loan amount to function main. Write value function getMonth Payment that has no parameters, uses a validation loop to prompt for and get from the user the monthly payment in the range $50-750, and returns the monthly payment to function main. Write value function getInterestRate that has no parameters, uses a validation loop to prompt for and get from the user the annual interest rate in the range 1-6%, and returns the interest rate to function main. Here are the first lines of the three functions:

User Caseygrun
by
3.9k points

1 Answer

1 vote

Answer:

Check the explanation

Step-by-step explanation:

CODE:

#include <iostream>

#include <iomanip>

using namespace std;

double getLoanAmount() { // functions as per asked in question

double loan;

while(true){ // while loop is used to re prompt

cout << "Enter the car loan amount ($2,500-7,500): ";

cin >> loan;

if (loan>=2500 && loan <= 7500){ // if the condition is fulfilled then

break; // break statement is use to come out of while loop

}

else{ // else error message is printed

cout << "Error: $"<< loan << " is an invalid loan amount." << endl;

}

}

return loan;

}

double getMonthlyPayment(){ // functions as per asked in question

double monthpay;

while(true){ // while loop is used to re prompt

cout << "Enter the monthly payment ($50-750): ";

cin >> monthpay;

if (monthpay>=50 && monthpay <= 750){ // if the condition is fulfilled then

break; // break statement is use to come out of while loop

}

else{ // else error message is printed

cout << "Error: $"<< monthpay << " is an invalid monthly payment." << endl;

}

}

return monthpay;

}

double getInterestRate(){ // functions as per asked in question

double rate;

while(true){ // while loop is used to re prompt

cout << "Enter the annual interest rate (1-6%): ";

cin >> rate;

if (rate>=1 && rate <= 6){ // if the condition is fulfilled then

break; // break statement is use to come out of while loop

}

else{ // else error message is printed

cout << "Error: "<< rate << "% is an invalid annual interest rate." << endl;

}

}

return rate;

}

int main() {

cout << setprecision(2) << fixed; // to print with 2 decimal places

int month = 0; // initializing month

// calling functions and storing the returned value

double balance= getLoanAmount();

double payment= getMonthlyPayment();

double rate= getInterestRate();

rate = rate /12 /100; // as per question

// printing as per required in question

cout << "Month Balance($) Payment($) Interest($) Principal($)"<< endl;

while(balance>0){ // while the balance is more than zero

month = month + 1; // counting Months

// calculations as per questions

double interest = balance * rate;

double principal = payment - interest;

balance = balance - principal;

// printing required info with proper spacing

cout <<" "<< month;

cout <<" "<< balance;

cout <<" "<< payment;

cout <<" "<< interest;

cout <<" "<< principal << endl;

}

cout << "Months to repay loan: " << month << endl; // displaying month

cout << "End of Banker Bisons";

Kindly check the output in the attached image below.

2) [13 points] You’ve been hired by Banker Bisons to write a C++ console application-example-1
User Aphax
by
3.9k points