locked
Need some help. RRS feed

  • Question

  • it is a old homework assignment i am trying to do for practice purposes.

    1.1  You are asked to develop an application to calculate savings for a number of time periods. Assume in the beginning of each month the user plans to save x amount of dollars to the investment account that pays r % of annual interest compounded monthly. (To find monthly rate you smohould divide annual rate by 12 months). Your application should take as inputs monthly payment (the amount user intends to save each month), and the annual rate (as percent). When the user clicks “next month” button, the program should calculate the new balance, and display the number of months passed. Initially, the balance is zero, and number of months is also 0. 

    here is what i got.

         private void button1_Click(object sender, EventArgs e)
            {
                decimal interest = decimal.Parse(textBox2.Text);
                int month = 1;
                int count = 1;
                decimal balance;

                if (decimal.TryParse(textBox1.Text, out balance))
                {
                    while (count<=month)
                    {
                        balance = balance + (interest * balance);
                        count = count + 1;
                        month = month + 1;
                    }
                    label6.Text = balance.ToString("c");
                }
                else
                {
                    MessageBox.Show("invalid value for balance");
                }
            }


     seems not working at all. there must be some logic error....i just can't figure it by myself.


    Saturday, November 28, 2015 5:51 PM

Answers

  • Hi Issaclconic,

    Your main logic error/problem in this code is with these two parts: 

     while (count<=month)
    and this:
     count = count + 1;
     month = month + 1;


    ... As you can see, you are incrementing both month and count variables... and the while loop condition/statement is (count <= month).. So problem is that count will never be less or equal to month, because both of them are incremented by +1 in every loop..

    Best regards!
    (If this was helpful for you propose it as an answer)


    • Edited by Almir VukMVP Sunday, November 29, 2015 12:06 AM
    • Proposed as answer by Almir VukMVP Sunday, November 29, 2015 12:06 AM
    • Marked as answer by IssacIconic Sunday, November 29, 2015 2:40 AM
    Sunday, November 29, 2015 12:05 AM

All replies

  • You have a problem with:

    while (count <= month)

    If you are incrementing both count and month at the same time, when will this condition ever be false?

    Saturday, November 28, 2015 6:19 PM
  • seems not working at all. there must be some logic error....i just can't figure it by myself.

    https://msdn.microsoft.com/en-us/library/y740d9d3.aspx?f=255&MSPPError=-2147217396

    Saturday, November 28, 2015 7:19 PM
  • Hi Issaclconic,

    Your main logic error/problem in this code is with these two parts: 

     while (count<=month)
    and this:
     count = count + 1;
     month = month + 1;


    ... As you can see, you are incrementing both month and count variables... and the while loop condition/statement is (count <= month).. So problem is that count will never be less or equal to month, because both of them are incremented by +1 in every loop..

    Best regards!
    (If this was helpful for you propose it as an answer)


    • Edited by Almir VukMVP Sunday, November 29, 2015 12:06 AM
    • Proposed as answer by Almir VukMVP Sunday, November 29, 2015 12:06 AM
    • Marked as answer by IssacIconic Sunday, November 29, 2015 2:40 AM
    Sunday, November 29, 2015 12:05 AM