By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,107 Members | 1,607 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,107 IT Pros & Developers. It's quick & easy.

where is the error?

P: n/a
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

Sep 25 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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++)
Sep 25 '07 #2

P: n/a
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?
Sep 25 '07 #3

P: n/a
vinnie <ce**********@gmail.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 - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 25 '07 #4

P: n/a
On Sep 25, 6:14 pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
>
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.com>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

Sep 25 '07 #5

P: n/a
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_value" 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_futuro" over and over. The
inputs to the "alfa" calculation don't change, and so the inputs for
"valore_futuro" don't change either ("alfa" being the only one modified
within the loop). Thus the calculated value for "valore_futuro" 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
Sep 25 '07 #6

P: n/a
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...@gmail.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

Sep 26 '07 #7

P: n/a
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...@gmail.comwrote:
How do i debug step by step?
sorry, i'm just at the begging

Sep 26 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.