where is the error?

  • Thread starter Thread starter vinnie
  • Start date Start date
V

vinnie

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
 
vinnie said:
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++)
 
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?
 
vinnie said:
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?
 
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?

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
 
vinnie said:
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
 
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!!
 
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!!

How do i debug step by step?
sorry, i'm just at the begging
 
vinnie said:
How do i debug step by step?
sorry, i'm just at the begging

Using the Visual Studio debugger. See the documentation for details,
but you are looking for topics like "breakpoint" (allows you to tell the
debugger a specific place where it should stop execution and allow you
to take control) and "step over", "step into", etc. which are ways of
controlling the execution of statements so that you can monitor the
results as each statement executes (for example, looking at the values
in each variable).

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top