Computing Loans

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
#include <iostream>
#include <cmath>
#include <iomanip>

using namespace std;

double firstfunction (double Ray, double Loan, double interest, double ments, double tym, double rate)

{ 
	rate = rate / 100;
	interest = (rate / ments ); 
	Ray = (( Loan * interest )/ 1 - (pow(1 + interest, - (ments * tym))));

		return Ray; 
}

double secondfunction (double Lp, double Ray, double interest, double rate, double ments, double tym, double amnt)

{ 
	rate = rate / 100;
	interest = ( rate / ments );
Lp = Ray * ( 1 - pow( 1+ interest, -(ments * tym - amnt))) / interest; 

	return Lp;
}

int main ()
{
	 double rate; 
	 double ments; //number of payments per year ments = 12 
	 double tym; // length of years loan is taken out for
	 double amnt; // amount of payments made
	 double Loan; // Loan amount
	double Lp = 0; // unpaid balance
	double Ray = 0; // Periodic payment
		
	cout << "Enter the loan amount: ";
	cin >> Loan;
	cout << endl; 

	cout << "Enter the interest rate per year as a percentage: ";
	cin >> rate;
	cout << endl; 

	cout << "Enter the number of payments per year: ";
	cin >> ments;
	cout << endl;

	cout << "Enter the number of years for the loan: ";
	cin >> tym;
	cout << endl;
	
	double interest = 0;
	Ray = firstfunction (Loan, interest, ments, tym, rate, Ray);
	
	cout << "The periodic payment is: " << Ray;
	cout << endl;

	cout << "Enter the number of payments made: ";
	cin >> amnt;
	cout << endl;
	
	Lp = secondfunction (Lp, Ray, interest, rate, ments, tym, amnt);

	cout << "The unpaid balance after " << amnt << " payment(s) is: "<< Lp;
	cout << endl;

	system ("pause");

	return 0;
}


So my problem is that I keep getting negative numbers for return, my teacher suggested a break statement after first function but from what I know break statements are only useable in switch and loops. Any shove in the right direction would be appreciated. Thanks!
look what you put in your first function
Ray = firstfunction (Loan, interest, ments, tym, rate, Ray);

double firstfunction (double Ray, double Loan, double interest, double ments, double tym, double rate)

Loan = enters user.
interest = 0.
ments = enters user.
tym = enters user.
rate = enters user.
Ray = 0.

in function
rate = rate / 100; are equal to rate = 0 / 100 ; rate = 0;
interest = (rate / ments ); are equal to interest = 0 / ments; interest = 0;
Ray = (( Loan * interest )/ 1 - (pow(1 + interest, - (ments * tym)))); are equal to
Ray = (( Loan * 0 )/ 1 - (pow(1 + 0, - (ments * tym)))); Ray = (0 - 1); Ray = -1

and not important what the user enters
Last edited on
Shini, I totally get what you're saying but if I declare Ray and Interest in any other way it gives me a initialization error for those two. Is there some way to make the function recognize user input in the first function??
1:
look here
1
2
Ray = firstfunction (Loan, interest, ments, tym, rate, Ray);
firstfunction (double Ray, double Loan, double interest, double ments, double tym, double rate)


variable Loan value from main function goes to Ray variable of firstfunction(), interest goes to Loan, ments goes to interest, tym goes to ments, rate goes to tym and Ray goes to rate. Do you really are intend to mix variables values? Or do you mix it not intentionally?

Or should this be
1
2
3
4
5
6
7
8
9
double firstfunction (double Loan, double interest, double ments, double tym, double rate)
{ 
        double Ray;
	rate = rate / 100;
	interest = (rate / ments ); 
	Ray = (( Loan * interest )/ 1 - (pow(1 + interest, - (ments * tym))));

	return Ray; 
}


Or maybe better
1
2
3
4
5
6
double firstfunction (double Loan, double interest, double ments, double tym, double rate)
{
	rate = rate / 100;
	interest = (rate / ments ); 
	return (( Loan * interest )/ 1 - (pow(1 + interest, - (ments * tym))));
}
Topic archived. No new replies allowed.