Hello jumbee2,
It is a good subject "Super confused" because I am super confused at times. When I first took a quick look at your variable names I noticed that you did a good job of naming the variables until I started working with the program and discovered that names ending with "B", "C", etc. my first thought was duplicate variables, but I did see a use of a second set in the if statements because this would allow you to keep information in the original variables in case you need them later.
To make things a little easier to understand I rearranged the beginning of the program and added some to the "cout" statements that you can either use or not. At least it gives you an idea of what you could do and some help of how to do it in the future. I refer to the "setw()"s.
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
|
#include <iostream>
#include <iomanip> // <--- Changed from "<fstream>" that is not needed.
#include <cmath>
#include <string>
//using namespace std; // <--- Best not to use.
int main()
{
int counter{}, totalPymts;
//double month{}; // <--- Not used.
double
mortgageAmt{ 100000.00 },
interestRate{ 4.5 }, loanLength{ 30 },
pymtYearly{ 12 };
double
totalLoan{},
totalInterest{},
mthlyPymt{},
decimalInterestRate{};
double
monthPaidInt{},
monthPrinciple{},
newmthBalance{},
mthBalance{};
// <--- Moved to if statements for "B".
//double mortgageAmtB{}, interestRateB{}, loanLengthB{}, decimalInterestRateB{};
//double totalLoanB{}, totalInterestB{}, totalPymtsB{}, mthlyPymtB{};
// <--- Also moved to if statements for "B", but not really needed.
//double totalInterestC{}, totalPymtsC{};
//double loanLengthC{}, mthlyPymtC{};
double balanceA{}, balanceB{}, balanceC{}, balanceD{}; // <--- Not sure yet what these are used for.
char reply{'B'}; // <--- Should be a "char" not a "string", but the string will work.
constexpr size_t WIDTH{ 44 }; // <--- Added.
std::cout << "\n Welcome to Nadia's Mortgage Calculator!\n";
std::cout << "\n" << std::setw(WIDTH) << "Enter the amount you would like to borrow:" << " $" << mortgageAmt;
//std::cin >> mortgageAmt;
std::cout << "\n" << std::setw(WIDTH) << "Enter your interest rate:" << ' '/*std::setw(6)*/ << interestRate << '%';
//std::cin >> interestRate;
std::cout << "\n" << std::setw(WIDTH) << " How many payments will you make each year?:" << std::setw(3) << pymtYearly;
//std::cin >> pymtYearly;
std::cout << "\n" << std::setw(WIDTH) << " How long in years will your loan last:" << std::setw(3) << loanLength;
//std::cin >> loanLength;
decimalInterestRate = (interestRate / 100.0) / pymtYearly;
totalPymts = static_cast<int>(pymtYearly * loanLength);
balanceA = mortgageAmt * (pow((1 + decimalInterestRate), totalPymts)) * decimalInterestRate;
balanceB = (pow(1 + decimalInterestRate, totalPymts)) - 1;
mthlyPymt = balanceA / balanceB;
totalInterest = (mthlyPymt * totalPymts) - mortgageAmt;
totalLoan = mortgageAmt + totalInterest;
std::cout << std::fixed << std::showpoint << std::setprecision(2); // <--- Added.
std::cout << "\n\n" << std::setw(WIDTH) << "The total amount paid back will be:" << " $" << totalLoan << std::endl;
std::cout << std::setw(WIDTH) << "Your total amount paid in interest will be:" << " $" <<totalInterest << std::endl;
std::cout << std::setw(WIDTH) << "The amount of payments you will make is:" << ' ' << totalPymts << std::endl;
std::cout << std::setw(WIDTH) << "Your monthly payment will be:" << " $" << mthlyPymt << std::endl;
std::cout << "\n What else would you like to do?" << std::endl;
std::cout << "\n Would you like to:\n (A) change length of the loan \n (B) make an amortization table\n (C) exit" << std::endl;
std::cout << " Enter choice: ";
//std::cin >> reply;
|
For a little research and some help I have been using this site
https://www.calculator.net/amortization-calculator.html?cloanamount=100000&cloanterm=30&cinterestrate=4.5&printit=0&x=81&y=20 This is for a loan amount of 100,000.00 at 4.5% for 30 years. I can not say if it is the best or the worst, but it is something to go by to check your work and every little bit helps.
The comments in the code should help to understand what is happening.
I moved "totalPymts" from a "double" to the "int" because you only need the whole number here and it works out better in the long run. This did mean I had to change the line
totalPymts = pymtYearly * loanLength;
to
totalPymts = static_cast<int>(pymtYearly * loanLength);
because you are trying to store a "double" into an "int" and the compiler gives a warning about possible data loss because it will drop the decimal portion of the "double" and only store the whole number. Either way works because the result of the calculation is a whole number, but the second example would be more proper.
I changed "reply" from a "string" to a "char" because you are using only one letter and there is no need to use a string for this. FWIW, for what this variable is used for I tend to use "choice" for the name as it is a better description of what it is used for. Whether you use "choice" or "replay" it is up to you, but I would suggest that you be consistent in its use. When writing code I feel it helps when you use certain words for the same task like "choice" when you are making a menu choice.
Two things about the way the "doubles" are done.
First; by putting each name on a different line it helps a bit to understand what you have.
Second; you will notice that some of these variables are initialized with a number. This is a little trick you can use so that you do not have to enter information every time the program runs and you will always be using the same data so you can see if your calculations are correct. This along with changing the "cout" statements as I did and putting a comment on the "cin" statements saves you some time. You will see I did the same thing with "reply" and the "cin" statement used for it.
The other variables have a set of empty{}s which initializes the variables to "0.0" (for "double"s), zero (for "int"s), and '\' (for "char"s). "std::string"s are empty to start with and do not need initialized unless there is something you want in the string.
The next two sections I left there commented out, but eventually they should be removed.
Line 40 I added to help with the "setw()" on the input and output. Use if you want or if you are not ready for this yet you can do something different.
Lines 44 - 70 give you an idea of what you can do to format the input and output to something that looks nicer.
Line 61. Is the real reason for including "<iomanip>". "std::fixed" tells the "cout" statements to print a "decimal" number not a "scientific" number. The "std::showpoint" is used to print ".00" should it occur. The "std::setprecision(2)", the most important part, says to print only 2 decimal places. This along with "std::setw()" help to align the decimal point of each row in the same column. In another program you might need to change the "2" to a "3" or "4" or even larger depending on the need.
The next part is the if statement for "B". I need to change a few things before I go over it to show you what I have done. Based on
dhayden's post what you need to do is put the calculations inside the for loop before the cout statement. Also if you want to keep track of totals you will need to code for that before the cout statement. That part I have not worked on yet. Still trying to get the loop to work correctly.
There is a start for you for now.
Hope that helps,
Andy