help with initializing variables and system(pause), system(cls)

I'm writing a program for school to calculate the monthly payment of a mortgage for 15, 30, or 40 years. I keep getting the error message that some of my variables are being used without being initialized - mainly "downAmt". I had this error in another program and I was told by a tutor I needed to move the variables before or after some of my other code. (I don't remember where it was at the time or where I had to move it to and I no longer have that code because it turned out I was using cin statements when my professor was looking for us to use constants for that program.) I am also wondering if I am using system(pause) and system(cls) correctly as I can't find anything about them in my textbook or anything online that I fully understand. I can't test my program with them until I fix my variable problem.

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
  
#include <iostream>    // Library for input/ouput
#include <iomanip>     // Library for manipulators
#include <string>      // Library for strings
#include <cmath>       // Library for mathmatical functions
using namespace std;

// Constant declarations
const int MONTHS_PER_YR = 12;    // Number of months in a year

int main() 
{
	// Variable declarations
	float purchPrice;            // Purchase price of the home
	float downPercent;           // Percentage of down payment
	float downDecimal;           // Percentage of down payment as a deciaml
	float annualInt;             // Annual interest rate
	float monthlyInt;            // Monthly interest rate 
	float financeAmt;            // Amount finaced in dollars
    float monthlyPmt;            // Monthly payment in dollars
	float downAmt;               // Amount of down paymant in dollars
	float totalPmts;             // Total number of payments made over the life of the loan
	float totalInt;              // Total amount of interest paid over the life of the loan
	int loanTerm;                // Number of years in the loan term
	int numMonths;               // Number of months in loan term
	string familyName;           // Name of family purchasing home

    // Caluclations
	annualInt = annualInt * 100;                    // Coverts annual interest rate to a decimal
	monthlyInt = annualInt / MONTHS_PER_YR * 100;   // Calculates monthly interest as a decimal
	financeAmt = purchPrice - downAmt;              // Calculates finance amount
	downDecimal = downPercent * 100;                // Coverts down payment percentage to a decimal 
	downAmt = purchPrice * downDecimal;             // Calculates down payment amount
	numMonths = loanTerm * MONTHS_PER_YR;           // Converts the term of the loan to months
	monthlyPmt = monthlyInt * financeAmt * pow(1 + monthlyInt, numMonths) * monthlyInt / (pow(1 + monthlyInt, numMonths) - 1); // Calculates the monthly payment
	totalPmts = numMonths * monthlyPmt;             // Calculates the total number of payments over the life of the loan 
	totalInt = totalPmts - financeAmt;              // Calculates the total amount of interest over the life of the loan 

	// Prompts for input
	cout << "Please enter the name of the family purchasing the home." << endl;
	cout << "please enter the home's price." << endl;
	cout << "Please enter the down payment percentage." << endl;
	cout << "please enter the annual interest rate as a percentage." << endl;
	cout << "Please enter the term of the loan in years." << endl;
	
	// Input by user
	cin >> familyName;
	cin >> purchPrice;
	cin >> downPercent;
	cin >> annualInt;
	cin >> loanTerm;
	
	//Output of results
	cout << "Financing breakdown for the " << familyName << " family" << endl;
	cout << "Price of home:                 $" << purchPrice << endl;
	cout << "Amount down:                   $" << downAmt << endl;
	cout << "Amount financed:               $" << financeAmt << endl;
	cout << "Annual interest:                " << annualInt << "%" << endl;
	cout << "Monthly interest:               " << monthlyInt << "%" << endl;

	cout << "15 year loan:" << endl;
	cout << "Monthly payment:               $" << monthlyPmt << endl;
	cout << "Number of monthly payments:     " << numMonths << endl;
	cout << "Total payments:                 " << totalPmts << endl;
	cout << "Total Interest:                 " << totalInt << endl;
       
        system("pause");
        system("cls");

	cout << "30 year loan:" << endl;
	cout << "Monthly payment:               $" << monthlyPmt << endl;
	cout << "Number of monthly payments:     " << numMonths << endl;
	cout << "Total payments:                 " << totalPmts << endl;
	cout << "Total Interest:                 " << totalInt << endl;

        system("pause");
        system("cls");

        cout << "40 year loan:" << endl;
	cout << "Monthly payment:               $" << monthlyPmt << endl;
	cout << "Number of monthly payments:     " << numMonths << endl;
	cout << "Total payments:                 " << totalPmts << endl;
	cout << "Total Interest:                 " << totalInt << endl;

	return 0;
}.
Last edited on
You need to get the input from the user before you can do the calculations. Right now you're performing the calculations with uninitialized variables which will have some random garbage data in them.
Yea, I tried that before I posted and it's still giving me that error.
Move all of the calculations below the part where you get the input from the user.
Remember, these variables can't store formulas -- you have to do the computations after you actually have meaningful values for the variables.

And about system("<something here>");, this is a nice read:
http://www.cplusplus.com/articles/j3wTURfi/
In your code, notice the two lines of code in bold:

28
29
30
31
32
33
34
35
36
37
    // Caluclations
	annualInt = annualInt * 100;                    // Coverts annual interest rate to a decimal
	monthlyInt = annualInt / MONTHS_PER_YR * 100;   // Calculates monthly interest as a decimal
	financeAmt = purchPrice - downAmt;              // Calculates finance amount
	downDecimal = downPercent * 100;                // Coverts down payment percentage to a decimal 
	downAmt = purchPrice * downDecimal;             // Calculates down payment amount
	numMonths = loanTerm * MONTHS_PER_YR;           // Converts the term of the loan to months
	monthlyPmt = monthlyInt * financeAmt * pow(1 + monthlyInt, numMonths) * monthlyInt / (pow(1 + monthlyInt, numMonths) - 1); // Calculates the monthly payment
	totalPmts = numMonths * monthlyPmt;             // Calculates the total number of payments over the life of the loan 
	totalInt = totalPmts - financeAmt;              // Calculates the total amount of interest over the life of the loan  


You are using downAmt before you give it a value (regardless of whether or not you properly read user input later on).

Also: you are writing too many comments.
Here's a simple rule about comments: "write as few as possible, but as many as needed".

In other words, don't write useless comments. Example of useless comments:

1
2
3
4
5
6
7
8
#include <iostream>    // Library for input/ouput
#include <iomanip>     // Library for manipulators
#include <string>      // Library for strings
#include <cmath>       // Library for mathmatical functions
using namespace std;

// Constant declarations
const int MONTHS_PER_YR = 12;    // Number of months in a year 


Thank you catfish! I didn't even notice I put downAmt in another calculation before I defined it. I should've posted here a lot sooner! I would have saved myself some headaches.
Topic archived. No new replies allowed.