Compound Interest Function

Hi There-
Ok, after working on this for an embarrassing number of hours, I think I came up with something less cringe-worthy to ya'll "real" programmers.
Allow me to submit my humble and probably awful code.
It totally works, but now my issue is that I'm trying to make it go back to an initial question if the response is a negative number. I got it to say, "Hey! Don't put in a negative number!", but then it goes to the next prompt. Here's my current output:

** Welcome to the Consumer Loan Calculator **
How much would you like to borrow? $-100
Please enter a positive loan amount.
What is your annual percentage rate? %

I want it to go back to "How much would you like to borrow?" if the user input is negative and only go to the next question if the input is positive. What am I doing wrong now?


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
 #include <iostream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
using namespace std;

void get_input (double &principal, double &APR, double mon_pay);

int main()
{
    double loan; // principal
    double APR; // APR
    double mon_pay; //monthly payment

    cout << "** Welcome to the Consumer Loan Calculator **"<<endl;
    do {
        cout << "How much would you like to borrow? $";
        cin >>loan;
        if (loan < 0)
        cout <<"Please enter a positive loan amount.";
        }
    while (loan > 0);
    cout << "What is your annual percentage rate? %";
    cin >>APR;
    cout << "What is your monthly payment? $";
    cin >> mon_pay;

    APR = APR/100;
    get_input (loan, APR, mon_pay);

}

void get_input (double &principal, double &APR, double mon_pay)
{
    double total, add=0; // Total payment
    int tpay=1; //Total months of payment

    while (principal > 0)
    {
        add = principal * (APR/100);
        principal = ((principal+add) - mon_pay);
        tpay++;
    }

    total = mon_pay + principal;
    cout << "Your debt will be paid off after "<< tpay << " months, with a final payment of just $" <<setprecision(3)<<total<<endl;
    cout <<"Don't get overwhelmed with debt!"<<std::endl;

}
Last edited on
Topic archived. No new replies allowed.