some expert directions????!!!!

Hello everyone.
Thank you for taking your time in this topic.

I have to write a program that calculates a loan and prints out the the monthly payment and the balance left of the loan.

I get user input for:

Loan amount, Interest, Payments per year, total year loan and payments made.

i have writen this so far but it looks like i have a hard time with the function returning a value.

Please any advise or hint will be appreciated.

Thank you for your time.

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
87
88
89
90
91
92
93
94
95
96
97
// include statement(s).
#include <iostream>
#include <cmath>
#include <iomanip>

// using namespace statement.
using namespace std;
// Declare named constants, if necessary.

//functions prototypes
double monthlyPayment ();
double unpaidBalance  ();

int main()
{
		
	double    x;//the monthly payment from the function()
	double    y;//balance left on the loan from function()
	char      choice;//to start or exit the program

	cout << fixed << showpoint << setprecision (2);
	
	cout <<"Enter (Y) to calculate the monthly payment and"
	     <<" balance left on your morgage.\n\n" 
	     <<"Enter (Q) to quit the program.";
	cin  >> choice;
	cout << endl;

	if (choice == 'Y' || choice == 'y')
	{
		a  = monthlyPayment();
			
		cout <<"The monthly payment is : $ " << a << endl;

		y = unpaidBalance();

		cout <<"The balnce of the loan is: " << y << endl;

		cout <<"Enter (Y) to find periodic payment and"
		     <<" balance left on your morgage.\n\n" 
		     <<"Enter (Q) quit the program.";
	        cin  >> choice;
	        cout << endl;	
	
	}//end if loop
	else 

		cout <<"good bye. " << endl;

	return 0;
}

double monthlyPayment ()
{
	double loanAmount, rValue, i, r;
	int    m, t, n, monthlyRate;

	cout <<"Enter the loan amount: ";
	cin  >> loanAmount;
	cout << endl;

	cout <<"Enter the interest per year as a number: ";
	cin  >> r;
	cout << endl;
		
	cout <<"Enter the payments in one year: ";
	cin  >> m;
	cout << endl;

	cout <<"Enter the number of years for the loan: ";
	cin  >> t;
	cout << endl;
	
	i = r / (m * 100); 
	n = m*t;
	rValue = loanAmount * i / (1 - pow(1+i,-n));

 return rValue;
}


double unpaidBalance()
{
	double k, i, x, n, lValue;

	//i am not sure how to get the rValue from 1st function
	//and use it in the 2nd function
	x = monthlyPayment();
	
	lValue = x * ((1 - pow(1+i,n)) / i);
	
	cout <<"Enter the number of payments made: ";
	cin  >> k;
	cout << endl;

return lValue;
}
Last edited on
Precisely what is your problem?

PS: Don't mix input, output and calculations - that's bad style.
Last edited on
i cant get the return value from second function.

Well, your second function is a bit problematic, because you use the variables n and i inside them, without assigning them a value first. But other than that it should return a value, albeit a garbage one.
i have the value in the first function.

can i get them from there? im not sure...
Last edited on
I think you didn't yet quite comprehend the meaning of procedures/functions... Theoretically you could make these values global. Theoretically. Normally you don't want to do that (unless there is absolutely no other practical solution). Rather, you want to pass parameters between functions. If you don't know how to do that, consult your C++ book of choice or the tutorial on this site.
ok thanks for taking your time.
Topic archived. No new replies allowed.