|
-
July 14th, 2011, 02:19 PM
#1
Loop Problem
I am in college for programming, but my programming classes do not start until January. I am trying to work on C++ to get ready for my classes. I am currently in Personal Finance class and was intrigued by an interest calculator. I am not done the code because I need to add the compound interest section in the final loop. The problem I can't seem to fix is updating the minimum payment after each monthly payment. I also need to section off the part that chooses a set payment or a minimum payment. I just wanted to point out I am aware of this and know I need to fix it.
The problem: If I add payment=total*MinInterest(this is defined as .03 for 3% then the loop gets stuck. I also used the math::ceil() for the wrong reason. I was trying to find a way to trim the double variables down to two decimal places(0.00) Can someone explain why I am having the loop problem and what I need to work on as far as code. I take all your advice very seriously and am anxious to learn. Here is the code.
BTW I am using DEV-C++
Code:
#include <cstdlib>
#include <iostream>
#include "math.h"
#define year 12
#define MinInterest .03
#define DaysInCycle 30
using namespace std;
// Displays Program Header
void menu()
{
cout<<"@@@@@@@@@@@@@@@@@@@@\n"
<<"@ @\n"
<<"@ Credit @\n"
<<"@ Card @\n"
<<"@ Payoff @\n"
<<"@ Calculater @\n"
<<"@ @\n"
<<"@@@@@@@@@@@@@@@@@@@@\n";
}
//Gets Bill Total
double bill_total()
{
double total;
system("CLS");
menu();
cout<<"Please Enter The Total Amount:";
cin>>total;
total=ceil(total);
return total;
}
// Gets APR
double APR()
{
system("CLS");
menu();
double APR;
cout<<"Please Enter APR:";
cin>>APR;
APR=ceil(APR);
return APR;
}
//Gets User's Payment If PaymentSelection == 2
double GetPayment()
{
double ammount;
system("CLS");
menu();
cout<<"Please Enter Payment Ammount:";
cin>>ammount;
return ammount;
}
//Calculates Minimum Payment
double CalculateMinPayment(int total)
{
return (total*MinInterest);
}
//User Selects Set Ammount Or Minimum Payment Style
double PaymentSelection(int total)
{
int choice;
do{
system("CLS");
menu();
cout<<"1.Minimum Payment 2.Set Payment\n";
cin>>choice;
if(choice==1)
return CalculateMinPayment(total);
else if(choice==2)
return GetPayment();
}while(choice!=1||choice!=2);
}
//Finale Calculation
void calculate(double APR,double Payment,double Total)
{
//initilize
double TotalInterest=0,MonthlyInterest=0,Principal=0,AverageDailyBalance=0,TotalPaid=0;
double month=1.0;
//loop tell total is paid
while(Total>0){
//calculate monthly APR
APR=ceil(APR);
//calculate monthly interest
MonthlyInterest=(APR/year)/100;
//calculate Average Daily Balance
AverageDailyBalance=Total/DaysInCycle;
//Find Total Interest Paid
TotalInterest=AverageDailyBalance*MonthlyInterest;
//round total interest
TotalInterest=ceil(TotalInterest);
//Calculate principal paid
Principal=(Payment-TotalInterest);
//Calculate total for next month bill
Total=(Total-Principal);
//Keep track of total paid
TotalPaid=TotalPaid+Payment;
month++;}
system("CLS");
cout<<"It will take you "<<(month/12)<<" years to pay off the ammount.\n";
cout<<"And it will cost you "<<TotalPaid<<"$"<<" total\n";
}
int main(int argc, char *argv[])
{
menu();
double total=bill_total();
calculate(APR(),PaymentSelection(total),total);
system("PAUSE");
return EXIT_SUCCESS;
}
Last edited by josh26757; July 14th, 2011 at 02:45 PM.
-
July 14th, 2011, 02:39 PM
#2
Re: Loop Problem
I couldn't compile it. You may want to check the type on the function parameter called total that you're passing around. In main it's a double, but you're passing it as an int. I'm surprised your compiler isn't complaining.
-
July 14th, 2011, 02:47 PM
#3
Re: Loop Problem
ooops
I had made a change while testing. It is fixed now. Thanks for the quick response!
-
July 14th, 2011, 02:54 PM
#4
Re: Loop Problem
The compound interest is not there is why it will come out as 5100$ Just go to the main loop in Calculate and add
Code:
Payment=Total*MinInterest;
I added
Code:
cout<<Payment<<" "<<month<<endl;
to the end of the loop as well and the .00000 digits seems to be causing the problem. I do not understand why. The MinInterest is always .03. Maybe I am being stupid, but I do not understand what is going on.
-
July 14th, 2011, 02:57 PM
#5
Re: Loop Problem
BTW-Let me know what looks wrong in my programming grammar. Is my code formatted OK? What should I be working on?
I am currently working on Algorithms and data logic when I have time.
-
July 14th, 2011, 03:04 PM
#6
Re: Loop Problem
Isn't your compiler giving you warnings? I changed total from int to double but I'm still getting warning C4700: uninitialized local variable 'TotalPaid' used
Get rid of all the warnings and try again.
Formatting wise, use some white space.
Instead of
TotalInterest=AverageDailyBalance*MonthlyInterest;
TotalInterest = AverageDailyBalance * MonthlyInterest;
That seems much easier on the eye to me.
-
July 14th, 2011, 03:10 PM
#7
Re: Loop Problem
I am not getting any warnings?
TotalPaid is initialized at the beginning of calculate() What line is showing the error?
-
July 14th, 2011, 03:16 PM
#8
Re: Loop Problem
 Originally Posted by josh26757
I am not getting any warnings?
What compiler are you using? Brand and version?
Regards,
Paul McKenzie
-
July 14th, 2011, 03:17 PM
#9
Re: Loop Problem
I was trying to find a way to trim the double variables down to two decimal places(0.00)
Usually you should do this via output formatting rather than actually modifying the value of the variable.
However, you should also be aware that financial code is generally supposed to use integer math since it's exact (counting cents or tenths of cents), whereas floating point math is approximate. This doesn't really matter for a toy program but keep it in mind.
-
July 14th, 2011, 03:21 PM
#10
Re: Loop Problem
 Originally Posted by josh26757
I am not getting any warnings?
TotalPaid is initialized at the beginning of calculate() What line is showing the error?
You changed your code since your OP. I still get two warnings with VC2008 with the code you currently have.
You're still passing a double as an int apparently.
(110) : warning C4244: 'argument' : conversion from 'double' to 'int', possible loss of data
(73) : warning C4715: 'PaymentSelection' : not all control paths return a value
-
July 14th, 2011, 03:23 PM
#11
Re: Loop Problem
 Originally Posted by Lindley
Usually you should do this via output formatting rather than actually modifying the value of the variable.
However, you should also be aware that financial code is generally supposed to use integer math since it's exact (counting cents or tenths of cents), whereas floating point math is approximate. This doesn't really matter for a toy program but keep it in mind.
Interesting,
If you do not mind give me an example of using integer math. I may be misunderstanding you but how do I get cents with an integer( I am such a noob).
-
July 14th, 2011, 03:24 PM
#12
Re: Loop Problem
 Originally Posted by GCDEF
You changed your code since your OP. I still get two warnings with VC2008 with the code you currently have.
You're still passing a double as an int apparently.
(110) : warning C4244: 'argument' : conversion from 'double' to 'int', possible loss of data
(73) : warning C4715: 'PaymentSelection' : not all control paths return a value
Does this fix it?
Code:
#include <cstdlib>
#include <iostream>
#include "math.h"
#define year 12
#define MinInterest .03
#define DaysInCycle 30
using namespace std;
// Displays Program Header
void menu()
{
cout<<"@@@@@@@@@@@@@@@@@@@@\n"
<<"@ @\n"
<<"@ Credit @\n"
<<"@ Card @\n"
<<"@ Payoff @\n"
<<"@ Calculater @\n"
<<"@ @\n"
<<"@@@@@@@@@@@@@@@@@@@@\n";
}
//Gets Bill Total
double bill_total()
{
double total;
system("CLS");
menu();
cout << "Please Enter The Total Amount:";
cin >> total;
total = ceil(total);
return total;
}
// Gets APR
double APR()
{
system("CLS");
menu();
double APR;
cout << "Please Enter APR:";
cin >> APR;
APR = ceil(APR);
return APR;
}
//Gets User's Payment If PaymentSelection == 2
double GetPayment()
{
double ammount;
system("CLS");
menu();
cout << "Please Enter Payment Ammount:";
cin >> ammount;
return ammount;
}
//Calculates Minimum Payment
double CalculateMinPayment(double total)
{
return (total*MinInterest);
}
//User Selects Set Ammount Or Minimum Payment Style
double PaymentSelection(double total)
{
int choice;
do{
system("CLS");
menu();
cout << "1.Minimum Payment 2.Set Payment\n";
cin >> choice;
if(choice==1)
return CalculateMinPayment(total);
else if(choice==2)
return GetPayment();
}while(choice!=1||choice!=2);
}
//Finale Calculation
void calculate(double APR, double Payment, double Total)
{
//initilize
double TotalInterest=0, MonthlyInterest=0, Principal=0, AverageDailyBalance=0, TotalPaid=0;
double month=1.0;
//loop tell total is paid
while(Total>0){
//calculate monthly APR
APR = ceil(APR);
//calculate monthly interest
MonthlyInterest = (APR/year) / 100;
//calculate Average Daily Balance
AverageDailyBalance = Total / DaysInCycle;
//Find Total Interest Paid
TotalInterest = AverageDailyBalance * MonthlyInterest;
//round total interest
TotalInterest = ceil(TotalInterest);
//Calculate principal paid
Principal = (Payment-TotalInterest);
//Calculate total for next month bill
Total = (Total-Principal);
//Keep track of total paid
TotalPaid = TotalPaid + Payment;
month++;}
system("CLS");
cout<<"It will take you "<<(month/12)<<" years to pay off the ammount.\n";
cout<<"And it will cost you "<<TotalPaid<<"$"<<" total\n";
}
int main(int argc, char *argv[])
{
menu();
double total = bill_total();
calculate(APR(),PaymentSelection(total),total);
system("PAUSE");
return EXIT_SUCCESS;
}
-
July 14th, 2011, 03:26 PM
#13
Re: Loop Problem
 Originally Posted by josh26757
Does this fix it?
Doesn't fix not all control paths return a value.
-
July 14th, 2011, 03:27 PM
#14
Re: Loop Problem
I think you undercharge the interest:
Code:
//calculate Average Daily Balance
AverageDailyBalance=Total/DaysInCycle;
//Find Total Interest Paid
TotalInterest=AverageDailyBalance*MonthlyInterest;
I wish my credit cards worked that way!
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
July 14th, 2011, 03:28 PM
#15
Re: Loop Problem
I don't get stuck in any loop. What is the problem and how do we recreate it? Have you tried the debugger?
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
|