|
-
July 14th, 2011, 10:00 PM
#31
Re: Loop Problem
 Originally Posted by Lindley
You should separate the input and output format from the internal storage format. Even if you're counting cents internally (so that $157.03 becomes 15703 cents), that doesn't mean that you shouldn't be able to accept "157.03" as an input in dollars. You validate the input, then you convert it to your internal format. Similarly, you can convert your count of cents back to the conventional notation on ouput:
Code:
void outputmoney(ostream &out, int totalcents)
{
int dollars = totalcents/100;
int cents = totalcents%100;
out << "$" << dollars << "." << cents;
}
outputmoney(cout, 15703); // test call
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.
-
July 15th, 2011, 09:42 AM
#32
Re: Loop Problem
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;
}
-
July 15th, 2011, 10:40 AM
#33
Re: Loop Problem
 Originally Posted by josh26757
What would be the right way to implement this in a class?
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.
-
July 15th, 2011, 12:08 PM
#34
Re: Loop Problem
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;
}
Last edited by josh26757; July 15th, 2011 at 12:25 PM.
-
July 15th, 2011, 12:36 PM
#35
Re: Loop Problem
 Originally Posted by josh26757
I decided to put it in a struct just to use a class,
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.
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;
}
Regards,
Paul McKenzie
-
July 15th, 2011, 05:11 PM
#36
Re: Loop Problem
 Originally Posted by Paul McKenzie
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?
Perfect!!!
Thank you, that looks much better. I was unsure what the proper indention should be. You explained and showed it perfectly. I will definitely use that from now on.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|