Issue with loan calculation program.

Hi, I'm having a small problem with this program I'm trying to write. It gets to the payment period function then gives me the else message by default, then gives an error stating that, "payPerYear" is being used without being initialized. I've gone through and seem to be unable to find where exactly I missed it, as I've declared, "int payPerYear;" in the right locations (I think). It's also messing up prior to even getting to that function. I've been at this for hours and am pretty much at my wit's end. Would anyone mind telling me where exactly I'm messing this up?

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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
  //Library inclusions.

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#include <math.h>

using namespace std;

//Prototype Functions

string getName();
string getType();
double getAmount();
double getYears();
double getInterest();
string getPeriod();
int getPayPerYear(string payPeriod);
double getPayment(double interestRate, double yearsFinanced, int payPerYear, double amountFinanced);
void displayData(string clientName, string loanType, double interestRate, double yearsFinanced, string payPeriod, double amountFinanced, double payment);


int main()
{
	string clientName, loanType, payPeriod;
	double amountFinanced;
	double yearsFinanced;
	double interestRate;
	int payPerYear;
	double payment;

	cout << "LOAN PAYMENT \n \n";
	
	cout << "Enter loan information as prompted below or Q to quit: ";

	while(clientName != "Q" || clientName != "q")
	{

	clientName = getName();
	
		if(clientName == "Q" || clientName =="q")
				{
					break;
				}
	
	loanType = getType();
	amountFinanced = getAmount();
	yearsFinanced = getYears();
	interestRate = getInterest();
	payPeriod = getPeriod();
	payPerYear = getPayPerYear(payPeriod);
	payment = getPayment(interestRate, yearsFinanced, payPerYear, amountFinanced);

		displayData(clientName, loanType, interestRate, yearsFinanced, payPeriod, amountFinanced, payment);
	}
}

string getName()
	{
	string clientName;

		cout << endl << "Enter the name of Borrower: ";
		getline(cin, clientName, '\n');											//Obtaining client's name.

		return clientName;
	}

string getType()
	{
	string loanType;

	cout << "Type of Loan: ";
	getline(cin, loanType, '\n');													//Obtaining what type of loan is being lent.

	return loanType;
	}

double getAmount()
	{
	double amountFinanced;

	cout << "Amount Financed: ";
	cin >> amountFinanced;												//Obtaining the amount owed.
	return amountFinanced;
	}

double getYears()
	{
	double yearsFinanced;

	cout << "Years financed (Enter as a decimal): ";
	cin >> yearsFinanced;												//Obtaining years financed.
	return yearsFinanced;
	}

double getInterest()
	{
	double interestRate;

	cout << "Annual Interest Rate (Entered as a decimal): ";			//Obtaining interest rate.
	cin >> interestRate;
	return interestRate;
	}

string getPeriod()
	{
	string payPeriod;
	
	cout << "Payment Period (Weekly, Monthly, etc): ";
	getline(cin, payPeriod, '\n');											//Obtaining frequency of payment.

	if (payPeriod == "Monthly")
	{
		payPeriod = "Monthly";
	}

	else if (payPeriod == "Quarterly")
	{
		payPeriod = "Quarterly";
	}

	else if (payPeriod == "Semiannually")
	{
		payPeriod = "Semiannually";
	}

	else if (payPeriod == "Annually")
	{
		payPeriod = "Annually";
	}

	else
	{
		cout << "Invalid selection. Please enter Monthly, Semiannually, Quarterly, or Annually.";
	}

	return payPeriod;
	}

int getPayPerYear(string payPeriod)									//Determines how many payments are made per year.
	{
		int payPerYear;

		if(payPeriod == "Monthly")
		{
			payPerYear = 12;
		}
		else if(payPeriod =="Quarterly")
		{
			payPerYear = 4;
		}
		else if(payPeriod =="Semiannually")
		{
			payPerYear = 2;
		}
		else if(payPeriod == "Annually")
		{
			payPerYear = 1;
		}

		return payPerYear;
	}

/***********************************getPayment****************************
	Description: Calculates a loan payment amount.
		interestRate: The annual interest rate of the loan(percentage).
		yearsFinanced: The number of years financed.
		payPerYear: the amount of pay periods per year.
		loanAmount: the amount of the loan in US currency.
	precondition:
		interestRate is defined within the range 0% to 18%.
		yearsFinanced is defined within the range of 1 to 30.
		payPerYear is defined as 1, 2, 4, or 12.
		loanAmount is defined within the range $500 to $1,000,000.
		uses header file: cmath.
	postcondition:
		A payment amount has been calculated and returned.
*/

double getPayment(double interestRate, double yearsFinanced, int payPerYear, double amountFinanced)
	{
		double payment = (interestRate*(pow((1+interestRate), payPerYear))/(pow((1+interestRate), payPerYear))-1)*amountFinanced;
		return payment;
	}

void displayData(string clientName, string loanType, double interestRate, double yearsFinanced, string payPeriod, double amountFinanced, double payment)
{
	cout << fixed << setprecision(2);
	cout << endl << left << setw(26) << "Loan Payment Report For: " << right << setw(41) << clientName << endl;
	cout << left << setw(26) << "Type of Loan: " << right << setw(37) << loanType << endl;
	cout << left << setw(26) << "Amount Financed: " << setw(30) << "$" << right << setw(8) << amountFinanced << endl;
	cout << left << setw(26) << "Years Financed: " << right << setw(38) << yearsFinanced << endl;
	cout << left << setw(26) << "Payment Period: " << right << setw(40) << payPeriod << endl;
	cout << left << setw(26) << "Annual Interest Rate: " << right << setw(38) << interestRate << endl;
	cout << left << setw(26) << "Payment Amount: " << setw(30) << "$" << right << setw(8) << payment << endl;
	system("pause");
}
I think in your
 
int getPayPerYear(string payPeriod)
function, you need to initialize payPerYear. You have ifs and else ifs, but if none of those conditions are met then you have payPerYear with nothing in it and it being returned to main.

Edit: Works when initializing the variable in the function.
Last edited on
If I try to put "int payPerYear" into the prototype I receive a, "Does not take more than 1 argument" error. Correct me if I'm wrong (And I almost certainly am), but the way I read it, in your prototypes you essentially place what you're going to be passing inside the parenthesis, which in that instance would be payPeriod, which it would then read and assign a value to the variable "payPerYear" inside the function, then pass it back.

Did I misunderstand that?

EDIT: Could you tell me which line number you're referring to? In the function itself I've had payPerYear initialized as int, and in the prototype it's throwing errors. So maybe you could tell me which line you're initializing on so I can see where my mistake is.
Last edited on
Line #144

int payPerYear; needs to be int payPerYear = 0;
Alright, so now I've solved that issue, my last issue is with my math in the "double getPayment" function.

1
2
3
4
5
6
7
8
9
10
11
double getPayment(double interestRate, double yearsFinanced, int payPerYear, double amountFinanced)
	{
		double rate = 0;
		double n = 0;

		rate = (interestRate / payPerYear);
		n = (payPerYear*yearsFinanced);

		double payment = ((rate*pow((1+rate), n)/pow((1+rate), n)-1))*amountFinanced;
		return payment;
	}


Seems to return a negative number, which is obviously not right... I know that formula will return invalid results if the interest rate is 0, and I plan on using an if function to fix that, but I really can't see where the function is messing up here.

EDIT: A prior version of this code included a cout statement to ensure rate and n were returning appropriate values.
Last edited on
Topic archived. No new replies allowed.