Loan calc

Need some help with this code. It is pretty much a loan repayment calculator. The company only offers 10, 15, and 30 year fixed loans. The interest rates are based off the customers credit score. Based on the customer’s credit score, the program will randomly generate an interest rate based on the following ranges:

Rating Interest Rate
Excellent 2.75% - 4.00%
Good 4.01% - 6.50%
Fair 6.51% - 8.75%
Bad 8.76% - 10.50%

To receive credit for this assignment, certain programming features must be implemented in such a way as to demonstrate your knowledge of random number generation, use of enums, proper formatting of output, input error checking, switch statements, and if statements.

Must prompt user to enter loan amount and credit score. ensure that the credit score entered does not exceed 850. If a non-numeric, negative, or score that exceeds 850 is entered, prompt the user to re-enter the score.

If the credit score is below 300, display an error message to the user stating that the loan cannot be offered and exit the program. Valid terms are 10, 15, and 30 years. Any other numbers should be rejected, and the user should be prompted to re-enter an appropriate value.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <iomanip>  // setprecision() setw()
#include <cmath>    // pow(6, 2) 6^2



using namespace std;

int main()
{
    
    
    // Constant
    const int ONE     = 1,
    HUNDRED = 100,
    NUMBER_OF_MONTHS = 12;
    
    // Variables
    int N;
    
    float Rate,
    L,
    Payment,
    amount_paid_back,
    interest_paid;
    
    // Ask the user for Rate, number of paymets, and amount of the loan
    cout << endl;
    cout << "What is interest rate of the loan? %";
    cin >> Rate;
    cout << "What is the Amount of the loan? ";
    cin >> L;
    cout << "Number of payments? ";
    cin >> N;
    
    // Calculation
    Rate /= NUMBER_OF_MONTHS;
    
    Rate /= HUNDRED; // 4.25% == .0425
    
    // Payment = [Rate * (1 + Rate)^N / ((1 + Rate)^N - 1)] * L
    Payment = ((Rate) * pow(ONE + (Rate), N) / (pow(ONE + (Rate), N) - ONE)) * L;
    
    Rate *= HUNDRED; // .0425 == 4.25
    
    amount_paid_back = N * Payment;
    
    interest_paid    = (N * Payment) - L;
    
    // Display
    /************************************************
     *       Loan Amount:            $ 10000.00     *
     *       Monthly Interest Rate:          1%     *
     *       Number of Payments:             36     *
     *       Monthly Payment:        $   332.14     *
     *       Amount Paid Back:       $ 11957.15     *
     *       Interest Paid:          $  1957.15     *
     ************************************************/
    
    cout << setprecision(2) << fixed << right << endl;
    
    cout << "Loan Amount:           $";
    cout << setw(10) << L << endl;
    
    cout << "Monthly Interest Rate: ";
    cout << setw(10) << Rate << '%' << endl;
    
    cout << "Number of Payments:     ";
    cout << setw(10) << N     << endl;
    
    cout << "Monthly Payment:       $";
    cout << setw(10) << Payment << endl;
    
    cout << "Amount Paid Back:      $";
    cout << setw(10) << amount_paid_back << endl;
    
    cout << "Interest Paid:         $";
    cout << setw(10) << interest_paid;
    
    cout << endl << endl;
    
    
    // Terminate Program
    return 0;
}
Do you have a specific problem or question?

By the way I don't see any random number generation/usage, or any input validation as required by the assignment.

Hello jax16,

It may help to reformat the instructions. I came up with this:

The company only offers 10, 15, and 30 year fixed loans.

The interest rates are based off the customers credit score.
Based on the customer’s credit score, the program will randomly generate an interest rate based on the following ranges:

Rating Interest Rate
 • Excellent 2.75% - 4.00%  // Range based on credit score.
 • Good 4.01% - 6.50%
 • Fair 6.51% - 8.75%
 • Bad 8.76% - 10.50%

To receive credit for this assignment, certain programming features must be implemented in such a way as to
demonstrate your knowledge of
 • random number generation,
 • use of enums,
 • proper formatting of output,
 • input error checking,
 • witch statements,
 • and if statements.

Must prompt user to enter
 • loan amount and credit score.
 • ensure that the credit score entered does not exceed 850.
 • If a non-numeric, negative, or score that exceeds 850 is entered, prompt the user to re-enter the score.

If the credit score is below 300, display an error message to the user stating that the loan cannot be
offered and exit the program.

Valid terms are 10, 15, and 30 years. Any other numbers should be rejected, and the user should be prompted
to re-enter an appropriate value.


This breaks it up into smaller pieces and helps to see what you have to do. Plus you can cross off what you have done.

This might help to get you started:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <iomanip>  // setprecision() setw()
#include <cmath>    // pow(6, 2) 6^2

using namespace std;

int main()
{

    // Constant
    constexpr int ONE{ 1 },
        HUNDRED{ 100 },  // <--- These should be "constexpr" or "const".
        NUMBER_OF_MONTHS{ 12 };

    // Variables
    int N;  // <--- "N" is what??

    double Rate,
        L,  // <--- "principal" is a better name.
        Payment,
        amount_paid_back,
        interest_paid;

    // Ask the user for Rate, number of paymets, and amount of the loan
    cout << "\nWhat is interest rate of the loan? %";  // <--- Should be asking for credit score.
    cin >> Rate;  // <--- "rate" is a random number based on credit score.

    cout << "What is the Amount of the loan? ";
    cin >> L;

    cout << "Number of payments? ";  // <--- Should be asking for number of years, (10, 15 or 30).
    cin >> N;  // <--- numOfPayments = years * NUMBER_OF_MONTHS.

    return 0;  // <--- Not required, but makes a good break point.
}

A "double" is a better choice than "float", but even a "double" may not always give a proper answer.

This is the first small part of the program that needs to worked on first. As jlb noted this is where you need to work on validating the input.

Until this part is working properly there is not much point in the rest of the program without proper input.

Andy
Lines 14-16, I wouldn't use ONE or HUNDRED. 1 and 100 are clearer than ONE and HUNDRED, and you're never going to change ONE to be 2 or HUNDRED to be 75. So the reasons to avoid magic constants just don't apply.

It's always good to distinguish between the "user friend" values and the "computer friendly" ones. For example, lines 37 & 39 convert from "user friendly" annual percentages to computer-friendly monthly decimals. I would leave it that way, so I wouldn't convert back to user friendly values at line 44. In general, convert from user to computer values as soon as possible after input, and convert to user-friendly values for output only.
Topic archived. No new replies allowed.