Monthly Payment

Oct 18, 2012 at 3:10pm
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
// Reaper

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
    float L , monthly , interest , amount , annual , monthlyrate;
    int n;

    cout << "This is a monthly payments program.\n\n";

    cout << "Enter the loan amount: $";
    cin >> L;
    cout << "Enter the rate percent in decimal form: ";
    cin >> annual;
    cout << "Enter the number of payments: ";
    cin >> n;

    cout.setf(ios_base::fixed , ios_base::floatfield);
    cout.precision(2);

    monthly = (L * pow(monthlyrate + 1, n) * monthlyrate) / (pow(monthlyrate + 1, n) - 1);
    amount = monthly * n;
    monthlyrate = annual / 12;
    interest= monthlyrate * n;


    cout << "\n\n";
    cout << "Loan Amount:                  $" << L << endl;
    cout << "Monthly Interest Rate:         " << monthlyrate << "%" << endl;
    cout << "Number Of Payments:            " << n << endl;
    cout << "Monthly Payment:              $" << monthly << endl;
    cout << "Amount Paid Back:             $" << amount << endl;
    cout << "Interest Paid:                $" << interest << endl;

    return 0;
}


Im having trouble with line 24, when i run my program for monthly payment and amount paid back i get -nan.

I know my line 24 is wrong somewhere but i dont know where, heres the formula , anyone see whats wrong.

Payment = rate * (1 + rate)^n / (1 + rate)^n * L i think this is correct.
Oct 18, 2012 at 3:14pm
You haven't initialised monthlyrate.

Initialisation is the of the main sources of error in programming, so always be aware of it.

I tend to declare my variables 1 per line, initialise them to something (as long as this doesn't cause problems later), and put a comment to say what the variable means.

HTH

Edit: You probably need to calculate what the monthly rate is.
Last edited on Oct 18, 2012 at 3:18pm
Oct 18, 2012 at 3:24pm
"Run-Time Check Failure #3 - The variable 'monthlyrate' is being used without being initialized." - VS2012

I initialized it like the guy said from above, float L; float monthly; float interest; and the program still crashed.

Then I set default values because if you don't, it'll grab whatever memory was left in the RAM and use it.

Since you're passing monthlyrate to a function before it ever has a value, it crashes. (pow(x, y) is a function)

So the initialization you used would have been fine, if you had just set some default values, I think. Just to be safe, it'd be good programming practice to declare your variables line - by - line and set default values for each one.

example:
float L = 0;
float monthly = 0;
float monthlyrate = 0; etc...etc...
Last edited on Oct 18, 2012 at 3:30pm
Oct 18, 2012 at 3:25pm
ok i will ,and what about my interest, my interest = monthlyrate * n, but that has to be wrong because its not showing up the right answer.

and by the way my monthlyrate works now.

if i enter

10000 for l
.12 for annual
36 for n

my loan to amount paid back is correct my interest is suppose to be 1957.16 but i get 0.36 what else am i suppose to have in interest.
Oct 18, 2012 at 4:39pm

codiddle wrote:
I initialized it like the guy said from above, float L; float monthly; float interest; and the program still crashed.


That is declaration, not initialisation. Initialisation means assigning a value, which you are calling a default value.

Default value has a special meaning in C++, namely in assigning a value to a parameter of a function, which means that the user does not have to supply an argument in the function call, unless they want a value different to the default value.

@Reaper1

As I said above you need to calculate the value of monthlyrate, before you use it on line 24.
Oct 18, 2012 at 10:06pm
I did initialize it and it works for me, the only part that wrong it the interest part. If i enter the the numbers i wrote above interest should show 1957.16 , but i get 0.36 everytime, i know im missing something in interest calculation but i dont know what heres my updated code.

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
// Reaper

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
    float L , monthly , interest , amount , annual , monthlyrate;
    int n;

    cout << "This is a monthly payments program.\n\n";

    cout << "Enter the loan amount: $";
    cin >> L;
    cout << "Enter the rate percent in decimal form: ";
    cin >> annual;
    cout << "Enter the number of payments: ";
    cin >> n;

    cout.setf(ios_base::fixed , ios_base::floatfield);
    cout.precision(2);

    monthlyrate = annual / 12;
    monthly = (L * pow(monthlyrate + 1, n) * monthlyrate) / (pow(monthlyrate + 1, n) - 1);
    amount = monthly * n;
    interest= monthlyrate * n;


    cout << "\n\n";
    cout << "Loan Amount:                  $" << L << endl;
    cout << "Monthly Interest Rate:         " << monthlyrate << "%" << endl;
    cout << "Number Of Payments:            " << n << endl;
    cout << "Monthly Payment:              $" << monthly << endl;
    cout << "Amount Paid Back:             $" << amount << endl;
    cout << "Interest Paid:                $" << interest << endl;

    return 0;
}


my problem now is line 27 the output is wrong.
Oct 18, 2012 at 11:48pm
Put comments in to help you follow the numbers


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
// Reaper

#include <iostream>
#include <math.h>
using namespace std;

int main()
{
    float L , monthly , interest , amount , annual , monthlyrate;
    int n;

    cout << "This is a monthly payments program.\n\n";

    cout << "Enter the loan amount: $"; // enter 10,000 here
    cin >> L;
    cout << "Enter the rate percent in decimal form: "; // you enter .12 here
    cin >> annual;
    cout << "Enter the number of payments: "; // 36 here
    cin >> n;

    cout.setf(ios_base::fixed , ios_base::floatfield);
    cout.precision(2);

    monthlyrate = annual / 12; // Monthlyrate = .12/12     (.1)
    monthly = (L * pow(monthlyrate + 1, n) * monthlyrate) / (pow(monthlyrate + 1, n) - 1);
    amount = monthly * n;
    interest= monthlyrate * n;  // interest = .1 * 36    (.36)


    cout << "\n\n";
    cout << "Loan Amount:                  $" << L << endl;
    cout << "Monthly Interest Rate:         " << monthlyrate << "%" << endl;
    cout << "Number Of Payments:            " << n << endl;
    cout << "Monthly Payment:              $" << monthly << endl;
    cout << "Amount Paid Back:             $" << amount << endl;
    cout << "Interest Paid:                $" << interest << endl;

    return 0;
}
Oct 19, 2012 at 12:22am
Ok i will , how can i get the right answer in the interest line 27, its outputting the wrong number. i know why it is, im just having trouble figuring out what the right equation would be.
Oct 19, 2012 at 12:31am
Before i do that, im trying to wrap my mind around how you concluded that your interest should be 1957.16 in total
Oct 19, 2012 at 1:20am
ok ill post the problem dont worry im not in school, im teaching myself c++ , c# , java, perl , ruby , assembly , and pretty much every other language.

heres the problem and the input for the problem and what it should look like.

18. Monthly Payments
The monthly payment on a loan may be calculated by the following formula:

monthly = (L * pow(monthlyrate + 1, n) * monthlyrate) / (pow(monthlyrate + 1, n) - 1);

Rate is the monthly interest rate, which is the annual interest rate divided by 12. (A 12
percent annual interest would be 1 percent monthly interest.) N is the number of payments
and L is the amount of the loan. Write a program that asks for these values and displays
a report similar to the following:

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

everything up to the interest is correct in my program but i cant get the interest to show 1957.15
Last edited on Oct 19, 2012 at 1:33am
Oct 19, 2012 at 12:23pm
Anyone got any ideas?
Oct 19, 2012 at 12:53pm
I am try to be fair dinkum (truthful, constructive) about this comment:

11957.15 - 10000.00 = 1957.15


HTH
Topic archived. No new replies allowed.