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?
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstdlib>
#include "Loan.h"
usingnamespace std;
usingnamespace 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;
}
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.
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.
// LoanCalc.cpp : main project file.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <cstdlib>
#include "Loan.h"
#include <cmath>
#include <iomanip>
usingnamespace std;
usingnamespace 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;
}