Hi all I posted a few days ago but the thread wont let me re-post into it or answer questions that were asked.
Somewhere in my code the compound interest and years stop working and give wrong numbers. It work well for single digit years but if someone puts in a say 100 years the loan interest and final cots skyrocket. Not sure where I went wrong and yer it is class work but just cant see where it went wrong. Code contains two separate parts one that gives you a monthly payment based off a loan and year with a stepped interest rate. the program 1 (1st option) is the one where I am having issues.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <windows.h>
#include <wincon.h>
#include <cstdbool>
#include <vector>
#include <ctgmath>
#include <limits.h>
#include <cctype>
#include <ctime>
#include <cstdlib>
#include <iomanip>
#include <ios>
#include "ConsoleApplication5-23.h"
usingnamespace std;
char DONE;
double LOAN = 1; // initialize & set variables all to a value of 1 or 0 for next efw lines
double INT_YR = 1;
double PAYMENT = 1;
double INT_MO = 1;
double INT_PD = 1;
double INT_PD2 = 00;
double NU_MO = 0;
double LOAN_VER, YEARS;
double HOW_MUCH = 1;
double BAL1 = 1;
double BAL2 = 1;
double PRINCIPAL = 1;
double TOTAL_INT, INT_DONE, FINALCOST, FINAL_COST, TOTAL_COST;
double FINALPMT = 0;
// Next lines are setting up headers to display based off user input requests.
string HEADER = "Program with 2 methods of Compound Interest loan comparison and total cost calculations; Single Loan cost over it's life and Variable Interest rate loan cost calculator. ";
string COL_HED = "Interest Rate \t Monthly Payment \t Total Cost ";
string HEADER1 = "Program to compare costs of loans from start to finish with interest rates increasing by 1/8% at a time. ";
string COL_HED1 = "Interest Rate \t Monthly Payment \t Total Cost ";
string HEADER2 = "Program to check length of time and costs for a loan based on amount of loan and the interest rate and the length of the loan in months. ";
string COL_HED2 = "Month\t PRINCIPAL Paid\t Interest Paid \t Balance ";
int main()
{
system("mode 650");
system("color 0D");
PROGRAM_BEGIN:
cout << HEADER << endl; // these lines display all the information the user needs to know
cout << '\n';
cout << " Please type in which method you want to do, type in a #1 and enter for a list of total loan costs or type in #2 for a span and overall payment plan of a single loan. ";
cout << '\n';
cin >> LOAN_VER;
if (LOAN_VER == 1) // User checks to see what they want to do single or multiple interest values
{
goto PROGRAM_1BEGIN;
}
if (LOAN_VER == 2)
{
goto PROGRAM_2BEGIN;
}
PROGRAM_2BEGIN:
cout << HEADER2 << endl; // these lines display all the information the user needs to know for the 2nd single loan over life span.
cout << '\n';
cout << " Please type your LOAN amount in dollars and cents without the dollar sign example 125000.00 ";
cout << '\n';
cin >> LOAN;
cout << " Please type your loan's YEARLY INTEREST RATE % example 3.125% = .03125 ";
cout << '\n';
cin >> INT_YR;
cout << " Please type the MONTHLY Payment in Dollars and Cents ";
cout << '\n';
cin >> PAYMENT;
cout << COL_HED2 << endl; // this display all the column headers
cout << '\n';
BAL1 = LOAN;
INT_MO = (INT_YR / 12);
while (BAL1 > PRINCIPAL) // Loops the 2nd set value.
{
NU_MO++;
INT_PD = (BAL1 * INT_MO);
PRINCIPAL = (PAYMENT - INT_PD);
BAL2 = (BAL1 - PRINCIPAL);
HOW_MUCH = (INT_PD2 + INT_PD);
cout << setw(6) << NU_MO << setw(16) << fixed << setw(16) << setprecision(2) << PRINCIPAL << setw(16) << INT_PD << setw(16) << BAL2 << endl; // LOOP for the 2nd run of test single loan over life.
if (BAL2 > PAYMENT) // checking for loop value to set up final payment
{
BAL1 = BAL2;
}
else (BAL1 = PRINCIPAL); // final payment value setting
INT_PD2 = HOW_MUCH;
FINALPMT = BAL2;
}
NU_MO++;
INT_PD = (FINALPMT * INT_MO);
FINALCOST = (FINALPMT + INT_PD);
BAL2 = (BAL1 - PRINCIPAL);
HOW_MUCH = (INT_PD2 + INT_PD);
cout << setw(6) << NU_MO << setw(16) << fixed << setw(16) << setprecision(2) << FINALCOST << setw(16) << INT_PD << setw(16) << "0" << endl; // Output to screen table of values for single loan over life
TOTAL_INT = HOW_MUCH;
TOTAL_COST = (TOTAL_INT + LOAN);
cout << '\n';
cout << " You paid $" << TOTAL_INT << " for a loan of $" << LOAN << " after " << NU_MO << " months for total cost of $" << TOTAL_COST << endl; // Output to screen of the 2nd looping test for final values
cout << '\n';
cin.ignore();
cin.clear(); //clear out the User Inputs in case junk left over.
cout << " Do you want to continue? Press Y for YES Continue or N for No and exit. ";
cin >> DONE; // Ask user if they want to to exit
if (DONE == 'N' || DONE == 'n')
{
return 1;
}
else {
cout << '\n';
}
goto PROGRAM_BEGIN; // go back to beginning
PROGRAM_1BEGIN:
cin.ignore();
cin.clear(); //clear out the User Inputs in case junk left over.
cout << HEADER1 << endl; // these lines display all the information the user needs to know for the 1st test
cout << '\n';
cout << " Please type your LOAN amount in dollars and cents without the dollar sign example 125000.00 ";
cout << '\n';
cin >> LOAN;
cout << " Please type in the number of years of the loan, example 10 for 10 years. ";
cout << '\n';
cin >> YEARS;
cout << " Please type the starting value interest rate we will give you total costs based off of an increasing span of 3% in 1/8% increments. ";
cout << " Example type in a 5 for 5% or a 4.5 for 4.5%, then the program returns costs with a increments of 0.125% totaling costs over life of loan. ";
cout << '\n';
cin >> INT_YR;
cout << COL_HED1 << endl; // this display all the column headers
cout << '\n';
BAL1 = LOAN; // Setting equal for dual ability same inputs.
INT_DONE = ( INT_YR + 3 ) ;
INT_YR = (INT_YR - 0.125);
while (INT_YR < INT_DONE) // External loop that increments the interest by 1/8%
{
INT_YR = (INT_YR + 0.125);
PAYMENT = (LOAN *(INT_YR/100)) / ((12 * (1- pow((1/1+INT_YR/1200),(-12*YEARS))))) ;
while (BAL1 > PRINCIPAL) // INTERNAL Loop below and the formulas that find for the payment and the final costs
{
NU_MO++;
INT_MO = ((INT_YR / 12) / 100);
INT_PD = (BAL1 * INT_MO);
PRINCIPAL = (PAYMENT - INT_PD);
BAL2 = (BAL1 - PRINCIPAL);
HOW_MUCH = (INT_PD2 + INT_PD);
if (BAL2 > PAYMENT)
{
BAL1 = BAL2;
}
else (BAL1 = PRINCIPAL);
INT_PD2 = HOW_MUCH;
FINALPMT = BAL2;
}
NU_MO++; // Loop and Formulas for output to screen.
INT_PD = (FINALPMT * INT_MO);
FINALCOST = (FINALPMT + INT_PD);
BAL2 = (BAL1 - PRINCIPAL);
HOW_MUCH = (INT_PD2 + INT_PD);
TOTAL_INT = HOW_MUCH;
TOTAL_COST = (PAYMENT * 12 * YEARS);
cout << setprecision(3) << setw(6) << INT_YR << "% " << setw(16) << fixed << setw(16) << setprecision(2) << " $" << PAYMENT << setw(16) << " $" << TOTAL_COST << endl; // loop output to screen
}
cin.ignore();
cin.clear(); //clear out the User Inputs in case junk left over.
cout << " Do you want to continue? Press Y for YES Continue or N for No and exit. " ;
cin >> DONE; // Ask user if they want to to exit
if (DONE == 'N' || DONE == 'n')
{
return 1;
}
else {
cout << '\n';
}
goto PROGRAM_BEGIN; // go back to beginning
return 0;
}
I think you need to re-do this project. The variables are poorly named and in capitals. Generally you should use all capitals only for constants. Alos, you should not use goto unless you need to. Instead of goto, put that code into a function.
Hope that helps!
thanks but really new so no idea how to use functions, used caps only to help ID terms used for the program,
The problem I think is somewhere in the calculation how it is using the years perhaps I should limit how many years can be put in to stop the 100 year loan and that will solve some of it but I think the calculation is still off over 10 years.
If I select option 2 I get whacky results. Looking at the loop at line 103, you have 3 variables with balances (PRINCIPAL, BAL1 and BAL2), and 2 the record interest payment (INT_PD INT_PD2). That's way more than necessary. Simplify the code so that one variable tracks the balance of the loan and then see if that helps.
thanks but really new so no idea how to use functions
That int main() and everything between the { and the } is a function. You see how you goto to a label:? That's just like calling a function.
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
int main()
{
// code here
std::cout << "The area of a square with sides measuring 4 and 6 = " << Area(4,6) << "\n";
return 0; // int is a number
}
int Area(double width, double height)
{
return width*height;
}
As dhayden states simplifying your code will more than likely help.
most of it worked but more I messed with it worse it got, spent all day on a different problem that I finally got working after 8+ hours... This one above I have ignored for most part hoping someone could help.
If I enter 10000 (Loan amount), 140 (years) and 4%, The table looks reasonable. Can you give an example if where the results are bad?
Line 192 looks suspicious. (1/1+INT_YR/1200) is the same as 1+INT_YR/1200. Did you mean (1/(1+INT_YR/1200))?
You initialize the variables at the beginning of the program but not at the beginning of each loop. That means some (like NU_MO) may have the wrong value the second time around.
very good point on the NU_MO issue that retains the old month number so that 2nd time it is run the months start out where the OLD/1st run left off at. I found that issue prior but had not fixed it, tried to clear.inputs but didnt seem to do it so will move my initial value to under the start of the loops.
I will try the formula fix hope that does it as for some reason the final amounts seem way too high to me as at 30 or more years the amount you pay back was 2,5times the original loan amount but that could be correct.
thank you much will try it and report back!
EDIT IN:
I did what Dhayden suggested and the 2nd section about initialized the variables (over again) just be low program 2 start and DID FIX that issue.
I also looked on-line for the 1st loan problem and get proper numbers as it was written. Just those loan costs seem out of wack so guess I am too $ conservative to be able to think how much Banks Make... and BTW MS VS Community did not like the extra two brackets as shown above, do not knot why should simply ignore extra brackets as long as they are paired up.???
Think this is done and will close as fix in a day.