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

Thread: Loop Problem

  1. #16
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Loop Problem

    Quote Originally Posted by josh26757 View Post
    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).
    How many cents are in a dollar? 1 is 1 cent. 100 is 100 cents, or 1 dollar. 1000 is 10 dollars. Etc.

    Viggy

  2. #17
    Join Date
    Mar 2011
    Posts
    52

    Re: Loop Problem

    Quote Originally Posted by GCDEF View Post
    I don't get stuck in any loop. What is the problem and how do we recreate it? Have you tried the debugger?
    To recreate it put
    Code:
    Payment=Total*MinInterest;
    at the end of the loop in calculate.
    The reason for this is to reset the minimum payment for each month. If you also add
    Code:
    cout<<Payment<<" "<<month<<endl;
    system("PAUSE");
    The code does not actually get stuck the decimal points just keep getting longer. I just do not know how to fix this. I have searched for trimming double on goggle but found nothing. My problem is I do not understand how to do it. My incompetence is the problem. Just asking how is the correct way to do this.

  3. #18
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Loop Problem

    Quote Originally Posted by josh26757 View Post
    To recreate it put
    Code:
    Payment=Total*MinInterest;
    at the end of the loop in calculate.
    The reason for this is to reset the minimum payment for each month. If you also add
    Code:
    cout<<Payment<<" "<<month<<endl;
    system("PAUSE");
    The code does not actually get stuck the decimal points just keep getting longer. I just do not know how to fix this. I have searched for trimming double on goggle but found nothing. My problem is I do not understand how to do it. My incompetence is the problem. Just asking how is the correct way to do this.
    I mean what values does one enter at the prompts to exhibit this behavior. I didn't see it.

  4. #19
    Join Date
    Mar 2011
    Posts
    52

    Re: Loop Problem

    Quote Originally Posted by MrViggy View Post
    How many cents are in a dollar? 1 is 1 cent. 100 is 100 cents, or 1 dollar. 1000 is 10 dollars. Etc.

    Viggy
    I understand what you are saying just how do you implement it? Better than wasting your time, can you give me a link that explains this? I am fairly new so I need a basic explanation.

  5. #20
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Loop Problem

    Quote Originally Posted by josh26757 View Post
    I understand what you are saying just how do you implement it? Better than wasting your time, can you give me a link that explains this? I am fairly new so I need a basic explanation.
    Work with cents instead of dollars. 1 dollar and 11 cents becomes 111 cents. Divide by 100 when you need to output dollars and cents.

  6. #21
    Join Date
    Mar 2011
    Posts
    52

    Re: Loop Problem

    Quote Originally Posted by GCDEF View Post
    I mean what values does one enter at the prompts to exhibit this behavior. I didn't see it.
    I am entering 5000 for the bill, 18 for the interest and minimum payment for the selection. This is the example in my Personal Finance class.

  7. #22
    Join Date
    Mar 2011
    Posts
    52

    Re: Loop Problem

    Quote Originally Posted by GCDEF View Post
    Work with cents instead of dollars. 1 dollar and 11 cents becomes 111 cents. Divide by 100 when you need to output dollars and cents.
    I will work on changing over the code tonight. I can see there needs to be exception handling going on just in case the user tries to enter cents(157.03). I know I also need to add exception handlers for invalid input but my main focus was just practice for the basic equation and to get better with my code. I know college will help polish me and make me "think outside the box", but I want to get ahead for better grades and understanding.

  8. #23
    Join Date
    Mar 2011
    Posts
    52

    Re: Loop Problem

    Quote Originally Posted by VladimirF View Post
    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!
    lol!!!
    I am happy today and thinking positive
    Helps ease the pain!!!
    I know that is a lame way to recreate the days in a particular month and I need to write a function to correct it, but on the other hand I will wait tell a unhappy day

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

    Re: Loop Problem

    Quote Originally Posted by josh26757 View Post
    lol!!!
    I am happy today and thinking positive
    Helps ease the pain!!!
    I know that is a lame way to recreate the days in a particular month and I need to write a function to correct it, but on the other hand I will wait tell a unhappy day
    I am not talking about differences in months length (28..31 days).
    My point is that what you call AverageDailyBalance is NOT an average daily balance. It is about 30 times less, which makes your interest negligible. You are practically paying down the principal only. But since you do not check for the overpayment on the last month, your calculations result in 34 payments (33 * $150 is $4,950, which is less that your initial amount), whatever the interest is.
    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...

  10. #25
    Join Date
    Mar 2011
    Posts
    52

    Re: Loop Problem

    While I have so many peoples attention can I ask what would benefit me the most to study? My main concentration is going to be Java in my associates degree then it moves to c++ introduction at the end. The bachelors goes more in depth with c++ and implementation. I know a lot of you have probably been through all this. So what did you have the hardest time on and what do you wish you would have had previous experience with?

    I have been watching MIT lectures at acadimicearth.org. As well as videos from
    The University of Whales. Very good videos for beginners and a heads up for college. I am self taught so far and these videos are all the learning I have done. I have some O'Reilly books but that is it. Going to band practice, I will check the posts when I get back.

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

    Re: Loop Problem

    Quote Originally Posted by josh26757 View Post
    ...I am trying to work on C++ to get ready for my classes...
    Just wanted to note that, other than use of std::cin and std::cout, your code has very little to do with C++.
    What book did you say you are using?
    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...

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

    Re: Loop Problem

    Quote Originally Posted by josh26757 View Post
    I will work on changing over the code tonight. I can see there needs to be exception handling going on just in case the user tries to enter cents(157.03).
    Usually, this is not a case for writing an exception handler in C++. Making a typing mistake is not an "exceptional" condition. It happens all the time.

    I know in Java and other languages, they throw exceptions for any little thing that could go wrong. In the C++ world, this usage of exceptions is rarely done. If the code is guaranteed to do something based on a condition, and the guarantee cannot be met due to some circumstance, then that would be a case for exception handling.

    For example, an important file missing when it must (not should, but must) be there on the system, or expanding an array to a certain size and it fails due to lack of memory, etc. these would be considered exceptional conditions. Someone with fat fingers typing a number and hitting enter doesn't fit this, IMO.

    Regards,

    Paul McKenzie

  13. #28
    Join Date
    Mar 2011
    Posts
    52

    Re: Loop Problem

    Quote Originally Posted by VladimirF View Post
    I am not talking about differences in months length (28..31 days).
    My point is that what you call AverageDailyBalance is NOT an average daily balance. It is about 30 times less, which makes your interest negligible. You are practically paying down the principal only. But since you do not check for the overpayment on the last month, your calculations result in 34 payments (33 * $150 is $4,950, which is less that your initial amount), whatever the interest is.
    Oh I see what I did wrong. I need to add all the interest together or
    ex:
    Code:
    day[1] =571;
    day[2]=579;
    ect.....
    
    for(int i = 0; i<day.size();i++){total=total+day[i];}
    AverageDailyBalance = (total/day.size());

  14. #29
    Join Date
    Mar 2011
    Posts
    52

    Re: Loop Problem

    Quote Originally Posted by VladimirF View Post
    Just wanted to note that, other than use of std::cin and std::cout, your code has very little to do with C++.
    What book did you say you are using?
    Not really using a book. More like watching lectures. I understand c++ is all about object or classes. I do need to do this and will be a lot more comfortable with it after I start working on java. I should implement classes and objects now. Is that what you are recommending?

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

    Re: Loop Problem

    Quote Originally Posted by josh26757 View Post
    I can see there needs to be exception handling going on just in case the user tries to enter cents(157.03).
    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

Page 2 of 3 FirstFirst 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