where is the error?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • vinnie

    #1

    where is the error?

    Can anyone tell me why the for cycle does not work? I only get the
    multiplication, but the for does not start:


    protected double CalFutureValue( double initial_value, double
    interest_rate, int years)
    {
    double future_value =0;
    double alfa = 0;
    for (int i = 1; i <= years; i++)
    {
    alfa = (initial_value * (interest_rate / 100)) ;
    valore_futuro = (alfa + initial_value);
    }
    }

    the fact is that i don't get any error message, it's just not making
    the calculation.
    Thanks

  • Alistair George

    #2
    Re: where is the error?

    vinnie wrote:
    Can anyone tell me why the for cycle does not work? I only get the
    multiplication, but the for does not start:
    >
    >
    protected double CalFutureValue( double initial_value, double
    interest_rate, int years)
    {
    double future_value =0;
    double alfa = 0;
    for (int i = 1; i <= years; i++)
    {
    alfa = (initial_value * (interest_rate / 100)) ;
    valore_futuro = (alfa + initial_value);
    }
    }
    >
    the fact is that i don't get any error message, it's just not making
    the calculation.
    Thanks
    >
    It suggests you are not assigning years - have you tried for test purposes:

    years=100;
    for (int i = 1; i <= years; i++)

    Comment

    • Alistair George

      #3
      Re: where is the error?

      PS if you break and debug on the 'for' you will get the values and find
      out why the thing isnt working as well in any case but you knew that did
      you not?

      Comment

      • Jon Skeet [C# MVP]

        #4
        Re: where is the error?

        vinnie <centro.gamma@g mail.comwrote:
        Can anyone tell me why the for cycle does not work? I only get the
        multiplication, but the for does not start:
        >
        protected double CalFutureValue( double initial_value, double
        interest_rate, int years)
        {
        double future_value =0;
        double alfa = 0;
        for (int i = 1; i <= years; i++)
        {
        alfa = (initial_value * (interest_rate / 100)) ;
        valore_futuro = (alfa + initial_value);
        }
        }
        >
        the fact is that i don't get any error message, it's just not making
        the calculation.
        Thanks
        Well, that isn't your actual code - it wouldn't compile, as you don't
        have a return statement.

        Could you post your *actual* code, preferably in the context of a short
        but complete program which demonstrates the problem?

        --
        Jon Skeet - <skeet@pobox.co m>
        http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
        If replying to the group, please do not mail me too

        Comment

        • vinnie

          #5
          Re: where is the error?

          On Sep 25, 6:14 pm, Jon Skeet [C# MVP] <sk...@pobox.co mwrote:
          >
          Well, that isn't your actual code - it wouldn't compile, as you don't
          have a return statement.
          >
          Could you post your *actual* code, preferably in the context of a short
          but complete program which demonstrates the problem?
          >
          --
          Jon Skeet - <sk...@pobox.co m>http://www.pobox.com/~skeet Blog:http://www.msmvps.com/jon.skeet
          If replying to the group, please do not mail me too
          I'm just trying to calculate the final value, when given a number of
          years, an initial amount, and an interest rate, i get the final
          amount payed.
          I did inserted the "return future_value" but it did not work anyway.

          Thanks

          Comment

          • Peter Duniho

            #6
            Re: where is the error?

            vinnie wrote:
            I'm just trying to calculate the final value, when given a number of
            years, an initial amount, and an interest rate, i get the final
            amount payed.
            Yes. That general description is apparent from the code you posted.
            But that's not at all the information Jon asked you to provide.
            I did inserted the "return future_value" but it did not work anyway.
            It's obvious from the code you posted that the code you posted wasn't
            simply copy and pasted from the actual program. The missing return
            statement and the mixed naming ("future_val ue" versus "valore_futuro" )
            make that clear enough.

            So, any comment on the code you posted may or may not actually be
            relevant to whatever problem you're actually having. We'd be commenting
            on what you posted, not the code you're actually using.

            That said, in the code you posted, there is no variable that actually
            depends on the iteration of the loop. All that the loop does is keep
            calculating the same "alfa" and "valore_fut uro" over and over. The
            inputs to the "alfa" calculation don't change, and so the inputs for
            "valore_fut uro" don't change either ("alfa" being the only one modified
            within the loop). Thus the calculated value for "valore_fut uro" is
            exactly the same each time through the loop.

            I leave it to the CPAs to decide whether your algorithm, even if
            modified so that the code in the loop does useful work, is actually a
            proper calculation of "future value". But as a programmer I can tell
            you that the loop you posted is useless.

            If you want it to do something useful, you need to explain better the
            exact calculation you're trying to implement.

            And if you want anyone to comment on the code you're _using_ you need to
            actually post that code. And for best assistance you need to post code
            that is "concise-but-complete", meaning it contains _nothing_ not
            directly related to reproducing the problem, but it at the same time is
            ready to be compiled as-is, without any additional work on the part of
            someone trying to compile it.

            This latter is something Jon already asked you once to provide. If you
            expect any real help, you need to be willing to work with the people
            offering help to provide the details they need to help you.

            Pete

            Comment

            • chou

              #7
              Re: where is the error?

              hi:
              the reason may be :
              1st, years equal 0, so no cycle.
              2nd,the values of the initial_value and interest_rate is make the
              cycle seem doing nothing when you run this code.

              but ,suggest you debugging step by step!!


              On 9 26 , 5 49 , vinnie <centro.ga...@g mail.comwrote:
              Can anyone tell me why the for cycle does not work? I only get the
              multiplication, but the for does not start:
              >
              protected double CalFutureValue( double initial_value, double
              interest_rate, int years)
              {
              double future_value =0;
              double alfa = 0;
              for (int i = 1; i <= years; i++)
              {
              alfa = (initial_value * (interest_rate / 100)) ;
              valore_futuro = (alfa + initial_value);
              }
              >
              }
              >
              the fact is that i don't get any error message, it's just not making
              the calculation.
              Thanks

              Comment

              • vinnie

                #8
                Re: where is the error?

                On Sep 26, 2:42 am, chou <jimu8...@gmail .comwrote:
                hi:
                the reason may be :
                1st, years equal 0, so no cycle.
                2nd,the values of the initial_value and interest_rate is make the
                cycle seem doing nothing when you run this code.
                >
                but ,suggest you debugging step by step!!
                >
                On 9 26 , 5 49 , vinnie <centro.ga...@g mail.comwrote:
                >
                How do i debug step by step?
                sorry, i'm just at the begging

                Comment

                Working...