Help please

I need help with this assignment I do not know what im doing wrong.


Write a program that accepts as input:

The loan amount
The interest rate per year
The monthly payment.
(Enter the interest rate as a percentage. For example, if the interest rate is 7.2% per year, then enter 7.2.)

The program then outputs the number of months it would take to repay the loan.

(Note that if the monthly payment is less than the first month’s interest, then after each payment, the loan amount will increase.)

In this case, the program must warn the borrower that the Monthly payment is too low. The loan cannot be repaid.

Code Tags:

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>
using namespace std;

int main() {
    double loan;
    double rate;
    double payment;
    double month;
    int m;
    
    cout << "Enter your loan amount: " << endl;
    cin >> loan;
    cout << "Enter your interest rate per year: " << endl;
    cin >> rate;
    cout << "Enter your monthly payment: " << endl;
    cin >> payment;
    month = rate / 12;
    while(payment <(loan*month))
    {cout << "Monthly payment too low. Loan cannot be repaid." << endl;
     cout << "Enter your monthly payment: ";
     cin >> payment;
    }
    m = 0;
    while(loan > 0)
    {loan = loan -(payment-(loan * rate));
    m++;
    }
    cout << "At" << rate << "% and payments of $" << payment << " per month\nit will take" << m << "months to repay the lone\n";
    system("pause");
  
    return 0;
}
Last edited on
Use code tags to make your code easier to read, like this:



Code Tags:

[code]

//Your Code Here

[/code]



What's the issue with the code or where are you stuck?
Are you intending to enter your interest rate as a percentage or a decimal fraction?

Ah, look:
(Enter the interest rate as a percentage. For example, if the interest rate is 7.2% per year, then enter 7.2.)



But that is NOT what you are doing in these lines:
1
2
month = rate / 12;
while(payment <(loan*month))


If you enter 7.2 then your monthly payment would be (7.2/12) of the total loan. I'd call you a loan shark!

Percentages are in hundredths of a whole - you need to accommodate that in your code.


And as @Zapshe says:
- put your code in code tags;
- tell us exactly why you think it is wrong.

Last edited on
I think that one of the loops is incorrect because when I run it it skips line 18 and runs my first loop until it crashes. Other than that I don't know what is wrong with it.
> it skips line 18
line 18: cout << "Enter your monthly payment: " << endl;
¿that line doesn't print to screen?
¿or is the next line cin >> payment; the one that doesn't execute?

¿what's your input?


> runs my first loop until it crashes
¿what exactly do you mean by crash?
¿does the program terminate? ¿freeze? ¿prints a lot of garbage?
It doesn't print line 18, but I was able to fix it. The main problem revolved around the interest rate, but @lastchance helped me with that. Thank you to all that helped.
Registered users can post here. Sign in or register to post.