CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 3 123 LastLast
Results 1 to 15 of 36

Thread: Loop Problem

  1. #1
    Join Date
    Mar 2011
    Posts
    52

    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&#37 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.

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    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.

  3. #3
    Join Date
    Mar 2011
    Posts
    52

    Re: Loop Problem

    ooops
    I had made a change while testing. It is fixed now. Thanks for the quick response!

  4. #4
    Join Date
    Mar 2011
    Posts
    52

    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.

  5. #5
    Join Date
    Mar 2011
    Posts
    52

    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.

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    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.

  7. #7
    Join Date
    Mar 2011
    Posts
    52

    Re: Loop Problem

    I am not getting any warnings?
    TotalPaid is initialized at the beginning of calculate() What line is showing the error?

  8. #8
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Loop Problem

    Quote Originally Posted by josh26757 View Post
    I am not getting any warnings?
    What compiler are you using? Brand and version?

    Regards,

    Paul McKenzie

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Loop Problem

    Quote Originally Posted by josh26757 View Post
    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

  11. #11
    Join Date
    Mar 2011
    Posts
    52

    Re: Loop Problem

    Quote Originally Posted by Lindley View Post
    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).

  12. #12
    Join Date
    Mar 2011
    Posts
    52

    Re: Loop Problem

    Quote Originally Posted by GCDEF View Post
    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;
    }

  13. #13
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: Loop Problem

    Quote Originally Posted by josh26757 View Post
    Does this fix it?
    Doesn't fix not all control paths return a value.

  14. #14
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    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...

  15. #15
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    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?

Page 1 of 3 123 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured