Loan calculator using classes

Hi, I am stuck on this loan calculator project. I am not getting any correct output. I am new to oop and classes and am having trouble sending variables to and from them. I am getting nonsense output (i.e. 1-N54654s, etc.) How can I fix this? Do I need to create a vector or an array for the table?

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
 #include "stdafx.h"
#include <iostream>
#include <string>
#include <cstdlib>
#include "Loan.h"

using namespace std;
using namespace System;


int main()
{

	char ans;
	int i;
	bool running = true;
	double p = 0, in = 0;
	int t = 0;



	cout << "Welcome, this program calculates monthly payments based on principal, interest and term values." << endl;
	do{
		cout << "Please enter your principal amount (no commas, 0 to quit): ";
		cin >> p;
		if (p < 0)
		{
			cout << "Please input a usable value"<<endl;
			//running = false;
			continue;
			
		}


		cout << "Please enter your annual interest rate (0.0 - 0.25): ";
		cin >> in;

		if (in <0 || in > 0.25)
		{
			cout << "Please input a value in the indicated range."<<endl;
			//running = false;
			continue;
		}
		cout << "Please enter your term (in months): ";
		cin >> t;
		if (t <= 0)
		{
			cout << "Please input a usuble term length"<<endl;
			//running = false;
			continue;

		}

		Loan l = Loan(p, in, t);

		cout << "A monthly payment of $" << l.monthly_payment() << " is required to pay off a $" << p << " loan in " << t << " months at a " << in*100 << "% rate." << endl;

		cout << "Would you like to display the chart? (Y/N): ";
		cin >> ans;
		ans = toupper(ans);


		if (ans = 'Y')
		{
			cout << "Month\t\tBeginning\t\tPayment\t\tInterest\t\tEnding"<<endl;
			for (i = 0; i < t; i++)
			{
				cout<<l.month_num(i)<<"\t"<<l.Initial_amount(i)<<"\t"<<l.monthly_payment()<<"\t"<<l.interest_charge(i)<<"\t"<<l.Ending_amount(i)<<endl;
				 //l.Initial_amount(i) == l.Ending_amount(i);
			}

		}

	} while (running);

	system("Pause");
    return 0;
}



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
#include "stdafx.h"
#include "Loan.h"
#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>

using namespace std;
using namespace System;
int i;


Loan::Loan(double p, double in, int t)
{
	amounti = p;
	payment = (in + (in / ((pow((in + 1), month)) - 1)))*amounti;
	

	for (i = 0; i < t; i++)
	{
		if (i > 0)
		{
			//payment = (interest + (interest / ((pow((interest + 1), month)) - 1)))*amounti(i);
		}
	}
}
int Loan::month_num(int x)
{
	return month;
}
double Loan::Initial_amount(int x)
{

	return amounti;
}
double Loan::interest_charge(int x)
{
	return charge;
}
double Loan::monthly_payment()
{
	return payment;
}
double Loan::Ending_amount(int x)
{
	return amounte;
}





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
#pragma once

using namespace std;

class Loan
{
public:
	Loan(double p, double in, int t);
	double monthly_payment();
	int month_num(int x);
	double Initial_amount(int x);
	double interest_charge(int x);
	double Ending_amount(int x);
	

private:
	int term;
	double month;
	double prin_amount;
	double interest;
	double payment;
	double charge;
	double amounti;
	double amounte;
	

};

Last edited on
loancalc.cpp
Line 24: p,in,t are uninitialized variables. They contain garbage.

loan.cpp
line 16: interest, month, amount, term are all uninitialized variables.

main.cpp
Lines 30,41,49: These if statements are all useless. They output a message, then continue as if nothing is wrong.

For the uninitialized variables, do I just have to set them to 0? or something else? and how can i fix those if statements so that if they input an incorrect value it prompts them to input it again, not go back to the beginning as it does now? I updated my code a little bit, but I think my formula is wrong or i am sending it the wrong values because I am not getting the correct output, although now some of it is actual numbers.
Last edited on
For the uninitialized variables, do I just have to set them to 0?

Since you've moved the declaration of l to line 54, p, in and t now have values when l is instantiated.

how can i fix those if statements so that if they input an incorrect value it prompts them to input it again

The usual idiom for prompting for valid values is a do/while loop.
1
2
3
4
5
6
    do 
    {    cout << "Please enter principal amount: "; 
	  cin >> p; 
          if (p < 0) 
              cout << "Principal amount is invalid" << endl;
    } while (p < 0);




Ok, thanks! So do I have to put a do/while loop around every instance of an input from the user? I am still not getting the right value for the initial calculated value of the monthly payment, and when i decide to try and display the table I get the number of iterations I want, but mostly wrong data. A mix of actual calculated values and some nonsense again. What is happening here?
Ok, so I did some work on it and here is what I have. I am getting a correct answer now for the original output of the monthly payment, but the table is still incorrect. I get the right payment (since it is the same everytime), but any value that changes is a 0 for the entire table.

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
// LoanCalc.cpp : main project file.

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

using namespace std;
using namespace System;


int main()
{

	char ans;
	int i;
	double p = 0, in = 0;
	int t = 0;



	cout << "Welcome, this program calculates monthly payments based on principal, interest and term values." << endl;
	
		do{
			cout << "Please enter your principal amount (no commas, 0 to quit): ";
			cin >> p;
			if (p == 0)
			{
				cout << "Thanks for using my program" << endl;
				return 0;
			}
			if (cin.fail() || p<0)
				
				cout << "Please enter a valid amount"<<endl;
				cin.clear();
				cin.ignore();
		
		} while (p<0 || cin.fail());

		do{
			cout << "Please enter your annual interest rate (0.0 - 0.25, 0 will quit): ";
			cin >> in;
			in = in / 12.0;
			if (in == 0)
			{
				cout << "Thanks for using my program"<<endl;
				return 0;
			}

			if (in < 0 || cin.fail())
				cout << "Please enter a valid interest rate";

		} while (in < 0 || cin.fail());
	
		do{
			cout << "Please enter your term (in months, 0 will quit): ";
			cin >> t;
			if (t == 0)
			{
				cout << "Thanks for using my program"<<endl;
				return 0;
			}
			if (t < 0 || cin.fail())
				cout << "Please enter a valid term in months";
		} while (t < 0 || cin.fail());

		Loan l = Loan(p, in, t);

		cout << "A monthly payment of $" <<fixed<<setprecision(2)<< l.monthly_payment() << " is required to pay off a $" << p << " loan in " << t << " months at a " << in*100*12 << "% rate." << endl;

		cout << "Would you like to display the chart? (Y/N): ";
		cin >> ans;
		ans = toupper(ans);


		if (ans == 'Y')
		{
			cout << "Month\tBeginning\tPayment\tInterest\tEnding"<<endl;
			for (i = 0; i < t; i++)
			{
				cout<<l.month_num(i)<<"\t"<<fixed<<setprecision(2)<<l.Initial_amount(i)<<"\t"<<l.monthly_payment()<<"\t"<<l.interest_charge(i)<<"\t"<<l.Ending_amount(i)<<endl;
				 //l.Initial_amount(i) = l.Ending_amount(i);

			}

		}

	

	system("Pause");
    return 0;
}


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
#include "stdafx.h"
#include "Loan.h"
#include <iostream>
#include <string>
#include <cstdlib>
#include <cmath>

using namespace std;
using namespace System;
int i;


Loan::Loan(double p, double in, int t)
{
	
	charge = new double[t];
	amounti = new double[t];
	amounte = new double[t];
	month = new double[t];

	//amounti = p;
	//charge = amounti*in;
	payment = (in + (in / ((pow((in + 1), t)) - 1)))*p;
	//amounte = p-payment+charge[i];


	for (i = 0; i < t; i++)
	{
		if (i > 0)
		{
			charge[i] = amounti[i] * in;
			//amounte[i] = p - payment + charge[i];
			amounti[i] = amounte[i-1];
			
		}
		amounte[i] = amounti[i] - payment + charge[i];
	}
}
int Loan::month_num(int x)
{
	return month[i];
}
double Loan::Initial_amount(int x)
{

	return amounti[i];
}
double Loan::interest_charge(int x)
{
	return charge[i];
}
double Loan::monthly_payment()
{
	return payment;
}
double Loan::Ending_amount(int x)
{
	return amounte[i];
}




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
 
#pragma once

using namespace std;

class Loan
{
public:
	Loan(double p, double in, int t);
	double monthly_payment();
	int month_num(int x);
	double Initial_amount(int x);
	double interest_charge(int x);
	double Ending_amount(int x);
	

private:
	int t = 0;
	double* month;
	double p = 0;
	double in = 0;
	double payment = 0;
	double* charge = 0;
	double* amounti;
	double* amounte;
	

};
Last edited on
Topic archived. No new replies allowed.