Thanks! that is a great explanation. I will try and figure it out and change my code. That should fix the problem. The double is causing the problem here. Thank you.
Printable View
Ok-Other than the total being a couple dollars different than BankRate.com Everything seems to be working well with the new code. I double checked my numbers against their
((months x Payment)+ending balance) and mine was right. So I don't understand that, but here is the new code. I decided to put it in a struct just to use a class, but it is lame. What would be the right way to implement this in a class?
Code:#include <cstdlib>
#include <iostream>
#define year 12
using namespace std;
struct Calculate{
int GetTotal()
{
int Total;
std::cout<<"Please Enter The Total Amount:";
std::cin>>Total;
return Total*100;
}
int APR()
{
int APR;
cout<<"Please Enter APR:";
cin>>APR;
return APR*100;
}
int Payment()
{
int Payment;
cout<<"Please Enter Your Total Payment:";
cin>>Payment;
return Payment*100;
}
void CalculateFinal(int Payment,int APR,int Total)
{
int TotalInterest=0, MonthlyInterest=0, Principal=0, AverageDailyBalance=0, TotalPaid=0, month=0, FinalPayment=0;
do{
AverageDailyBalance = Total;
MonthlyInterest = (Total * (APR / year) ) / 10000;
Principal = Payment - MonthlyInterest;
Total = Total - Principal;
TotalPaid = TotalPaid + Payment;
TotalInterest = TotalInterest + MonthlyInterest;
month++;
}
while(Total>Payment);
month++;
FinalPayment = Total;
TotalPaid = TotalPaid+FinalPayment;
int Dollars = (TotalPaid)/100;
int Cents = (TotalPaid)%100;
//Final Display
system("CLS");
cout<<endl<<endl
<<" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
<<" @ It Will Take You "<<month<<" Months @\n"
<<" @ And Cost You "<<Dollars<<"."<<Cents<<"$ @\n"
<<" @ Costing "<<TotalInterest/100<<" Dollars In Interest @\n"
<<" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n";
}
};
int main(int argc, char *argv[])
{
Calculate me;
me.CalculateFinal(me.Payment(),me.APR(),me.GetTotal());
system("PAUSE");
return EXIT_SUCCESS;
}
You really don't need a class there, but generally you'd separate your I/O from your class. The numbers such as total should be members of class and would be set via the constructor or through accessor functions. Functions such as CalculateFinal would operate on the data members rather than arguments passed into it.
Like This?
Code:#include <cstdlib>
#include <iostream>
#define year 12
using namespace std;
class Calculate{
int Payment;
int APR;
int Total;
public:
Calculate::Calculate();
int GetTotal()
{
int total;
std::cout<<"Please Enter The Total Amount:";
std::cin>>total;
return total*100;
}
int apr()
{
int apr;
cout<<"Please Enter APR:";
cin>>apr;
return apr*100;
}
int payment()
{
int payment;
cout<<"Please Enter Your Total Payment:";
cin>>payment;
return payment*100;
}
void CalculateFinal()
{
int TotalInterest=0, MonthlyInterest=0, Principal=0, AverageDailyBalance=0, TotalPaid=0, month=0, FinalPayment=0;
do{
AverageDailyBalance = Total;
MonthlyInterest = (Total * (APR / year) ) / 10000;
Principal = Payment - MonthlyInterest;
Total = Total - Principal;
TotalPaid = TotalPaid + Payment;
TotalInterest = TotalInterest + MonthlyInterest;
month++;
}
while(Total>Payment);
month++;
FinalPayment = Total;
TotalPaid = TotalPaid+FinalPayment;
int Dollars = (TotalPaid)/100;
int Cents = (TotalPaid)%100;
//Final Display
system("CLS");
cout<<endl<<endl
<<" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
<<" It Will Take You "<<month<<" Months\n"
<<" And Cost You "<<Dollars<<"."<<Cents<<"$\n"
<<" Costing "<<TotalInterest/100<<" Dollars In Interest\n"
<<" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n";
}
};
Calculate::Calculate()
{
Total=GetTotal();
APR=apr();
Payment=payment();
}
int main(int argc, char *argv[])
{
Calculate me;
me.CalculateFinal();
system("PAUSE");
return EXIT_SUCCESS;
}
You should also indent your code a little better. Nested loops and function blocks should be easily detected, but your current formatting just flushes everything to the left. For example, your do-while() loop in function CalculateFinal. What is you had another loop inside of the do-while, or a set of if() statements?
Here is an example of proper indentation.
Regards,Code:#include <cstdlib>
#include <iostream>
#define year 12
using namespace std;
struct Calculate {
int GetTotal()
{
int Total;
std::cout<<"Please Enter The Total Amount:";
std::cin>>Total;
return Total*100;
}
int APR()
{
int APR;
cout<<"Please Enter APR:";
cin>>APR;
return APR*100;
}
int Payment()
{
int Payment;
cout<<"Please Enter Your Total Payment:";
cin>>Payment;
return Payment*100;
}
void CalculateFinal(int Payment,int APR,int Total)
{
int TotalInterest=0, MonthlyInterest=0, Principal=0, AverageDailyBalance=0, TotalPaid=0, month=0, FinalPayment=0;
do
{
AverageDailyBalance = Total;
MonthlyInterest = (Total * (APR / year) ) / 10000;
Principal = Payment - MonthlyInterest;
Total = Total - Principal;
TotalPaid = TotalPaid + Payment;
TotalInterest = TotalInterest + MonthlyInterest;
month++;
}
while(Total>Payment);
month++;
FinalPayment = Total;
TotalPaid = TotalPaid+FinalPayment;
int Dollars = (TotalPaid)/100;
int Cents = (TotalPaid)%100;
//Final Display
system("CLS");
cout<<endl<<endl
<<" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
<<" @ It Will Take You "<<month<<" Months @\n"
<<" @ And Cost You "<<Dollars<<"."<<Cents<<"$ @\n"
<<" @ Costing "<<TotalInterest/100<<" Dollars In Interest @\n"
<<" @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n";
}
}
;
int main(int argc, char *argv[])
{
Calculate me;
me.CalculateFinal(me.Payment(),me.APR(),me.GetTotal());
system("PAUSE");
return EXIT_SUCCESS;
}
Paul McKenzie