3 Replies - 984 Views - Last Post: 23 July 2012 - 07:17 AM Rate Topic: -----

#1 crohnsandme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 17-July 12

program skips command half way through?

Posted 23 July 2012 - 06:48 AM

hi all my task here is :

Suppose you borrow $1000 at 12% annual interest and make monthly payments of $100.
* Write a C# program to calculate how many months it will take to pay off this loan.
* (Each month you pay interest on the remaining balance. The interest rate is 1% per month,
* so the first month you pay $10 interest and $90 goes to reduce the balance to $910.
* The next month's interest is $9.10, and $90.90 is applied to reduce the balance, and so on.
* The last month's payment may be less than $100.)

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {


            double eom = 3000;
            double interest;
            double mint;
            int month = 0;
            int year = 12 ;


            Console.WriteLine(" you borrowed 1000, here is workout");

            while (eom > 0)
            {
                interest = (eom / 100);
                mint = (100 - interest);
                eom = (eom - mint);
                month++;
                
                Console.WriteLine("{0:F2} interest", interest);
                Console.WriteLine("{0:F2} mint", mint);
                Console.WriteLine(" {0:F2} end of month", eom);
                Console.WriteLine("{0} month", month);
                if (month == year)
                {
                    eom = (((eom / 100) * 12) + eom);
                    year = (year + 12);
                    Console.WriteLine(" {0} year", year);
                }
            }

            Console.WriteLine(" It will take {0} months", month);
            Console.ReadLine();


            // on the 36 month it doesnt add on the interest of eom????
            }



        }
    }


( i added the writelines in so when debugging i can check it works fine)

now i got the program working for what it asks but i wanted to upgrade the program to be able to add 12% interest once it hits the next year, for this i added the "year = (year + 12); and it works fine but if you debug this program step by step you will see at the 36 month it doesn't re add the interest, then when i changed the eom to 5000 it seemed to work perfect right through to end?

can anyone see why this has happened and help me out with a point in the right direction? thanks

Is This A Good Question/Topic? 0
  • +

Replies To: program skips command half way through?

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3569
  • View blog
  • Posts: 11,089
  • Joined: 05-May 12

Re: program skips command half way through?

Posted 23 July 2012 - 06:58 AM

As an aside: when dealing with money use decimal, rather than double: http://docs.oracle.c...g_goldberg.html
Was This Post Helpful? 1
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5509
  • View blog
  • Posts: 11,814
  • Joined: 02-June 10

Re: program skips command half way through?

Posted 23 July 2012 - 07:06 AM

Line 18 defines the while to be while eom is greater than 0
Since line 22 keeps dropping the value of eom then it seems clear that it stops calculating because it drops equal to or less then 0.
WHile you are debugging you want your locals and autos pallets open so you can see all these values.

crohnsandme: What this shows us is that you aren't familiar with breakpoints and how to debug your own code. Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


See FAQ # 2. (Click the SHOW button below)



TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated July 2012
Spoiler



This post has been edited by tlhIn`toq: 23 July 2012 - 07:06 AM

Was This Post Helpful? 1
  • +
  • -

#4 crohnsandme  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 17-July 12

Re: program skips command half way through?

Posted 23 July 2012 - 07:17 AM

hi thanks guys , ye im only 3-4 days into learning c# and i sadly am to old for a college course anymore so am learning from a book "computing with C# and the .net framework (i am getting the new version in 2 days time)"

yes in this section it teaches me to learn to debug , by which i was following the program step by step but i just couldnt see why it skipped the 36 month interest change yet worked fine when i did another eom which went over 48 months.

i will take my time and read through the section on FAQ and hopefully learn more


yes i will make sure to use dec instead of double for currency in future

thanks guys
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1