I have an assignment to create a mortgage calculator in C++. I have attempted to complete this but I cannot get it to compile. Any help would be appreciated. Here is what I have come up with:
#include <stdio.h>
#include <math.h>
using namespace std;
/* Begin program */
int main() {
double loanAmount = 200000;
double loanBalance;
int i = 0;
int [] term = {7,15,30};
double rate = {.0535, .0550, .0575};
double monthlyPayment = (loanAmount*(rate[i]/12))/(1-(Math.pow(1/(1+(rate[i]/12)),(term[i]*12))));
for (i = 0; i < term.length; i++) {
NumberFormat cformat = NumberFormat.getCurrencyInstance(); /* Format numbers to currency */
cformat.setMinimumFractionDigits(2);
NumberFormat pformat = NumberFormat.getPercentInstance(); /* Format numbers to percents */
pformat.setMinimumFractionDigits(2);
System.out.println("The initial loan amount is " + cformat.format(loanAmount));
System.out.println("and the length of the term is " + term[i] + " years");
System.out.println("and the monthly interest rate is " + pformat.format(rate[i]));
/* This is the calculation for the monthly mortgage payment */
System.out.println("The monthly payment for this loan will be " + cformat.format(monthlyPayment));
}
System.out.println("This program will calculate the monthly interest paid, principle paid, and ");
System.out.println("loan balance for the selected loan.");
System.out.println("Please choose one of the terms listed below");
System.out.println("1: 7 years at 5.35% interest");
System.out.println("2: 15 years at 5.5% interest");
System.out.println("3: 30 years at 5.75% interest");
/* User menu display */
int myInteger = readInteger("Please enter the number of your choice.");
/* User input */
switch(myInteger) {
case 1: {
loanBalance = loanAmount;
int displayMonth = 1;
int displayLoop = 0;
while (displayMonth <= (term[0]*12))
{
double interestPayment = loanBalance*rate[0];
double principlePayment = monthlyPayment-interestPayment;
loanBalance = (loanBalance-principlePayment);
NumberFormat cformat = NumberFormat.getCurrencyInstance();
cformat.setMinimumFractionDigits(2);
System.out.println("For Month " + displayMonth + ":");
System.out.println("Interest paid is " + cformat.format(interestPayment));
System.out.println("Principle applied is " + cformat.format(principlePayment));
System.out.println("The new loan balance is " + cformat.format(loanBalance));
displayMonth++;
displayLoop++;
/*This is where the scroll begins*/
if (displayLoop == 3)
{
System.out.println("Press Enter to scroll down...");
String output = readLine();
displayLoop = 0;
}
}
break;
}
case 2: {
loanBalance = loanAmount;
int displayMonth = 1;
int displayLoop = 0;
while (displayMonth <= (term[1]*12))
{
double interestPayment = loanBalance*rate[1];
double principlePayment = monthlyPayment-interestPayment;
loanBalance = (loanBalance-principlePayment);
NumberFormat cformat = NumberFormat.getCurrencyInstance();
cformat.setMinimumFractionDigits(2);
System.out.println("For Month " + displayMonth + ":");
System.out.println("Interest paid is " + cformat.format(interestPayment));
System.out.println("Principle applied is " + cformat.format(principlePayment));
System.out.println("The new loan balance is " + cformat.format(loanBalance));
displayMonth++;
displayLoop++;
if (displayLoop == 3)
{
System.out.println("Press Enter to scroll down...");
String output = readLine();
displayLoop = 0;
}
}
break;
}
case 3: {
loanBalance = loanAmount;
int displayMonth = 1;
int displayLoop = 0;
while (displayMonth <= (term[2]*12))
{
double interestPayment = loanBalance*rate[2];
double principlePayment = monthlyPayment-interestPayment;
loanBalance = (loanBalance-principlePayment);
NumberFormat cformat = NumberFormat.getCurrencyInstance();
cformat.setMinimumFractionDigits(2);
System.out.println("For Month " + displayMonth + ":");
System.out.println("Interest paid is " + cformat.format(interestPayment));
System.out.println("Principle applied is " + cformat.format(principlePayment));
System.out.println("The new loan balance is " + cformat.format(loanBalance));
displayMonth++;
displayLoop++;
if (displayLoop == 3)
{
System.out.println("Press Enter to scroll down...");
String output = readLine();
displayLoop = 0;
}
}
break;
}
default: {
System.out.println("Invalid Entry. Please try again.");
}
}
}
public static String readLine()
{
int character;
String response = "";
boolean StringDone = false;
while (!StringDone)
{
try
{
character = System.in.read();
if(character < 0 || (char)character == '\n')
StringDone = true;
else response = response + (char)character;
}
catch(java.io.IOException e)
{
StringDone = true;
}
}
return response;
}
public static int readInteger(String userprompt)
{
while(true)
{
System.out.print(userprompt + " ");
try
{
return Integer.valueOf(readLine().trim()).intValue();
}
catch(NumberFormatException e)
{
System.out.println("Invalid entry.");
}
}
}
}
sorry for the formatting errors. it didn't look like that before i copied it.
[moderator edit: That's because you're supposed to use code tags]