Re: Weird calculation
Re: Weird calculation
- Subject: Re: Weird calculation
- From: Andreas Doerr <email@hidden>
- Date: Thu, 24 Jan 2002 11:44:33 +0100
>
switch([list indexOfSelectedItem]) {
>
case 0:
>
loesung = wert1 + wert2;
>
case 1:
>
loesung = wert1 - wert2;
>
case 2:
>
loesung = wert1 * wert2;
>
case 3:
>
loesung = wert1 / wert2;
>
}
>
>
[calc setFloatValue:loesung];
>
>
--
>
>
Please note that "loesung" stands for the result, "wert1" and "wert2"
>
for value 1 and 2. "calc" is the textfield for the result.
>
>
The strange thing (for me) is that I always get wrong results!
>
Can somebody help me?
I guess that `case 34 works ;-) Your problem has nothing to do with
Coca. Basically you forgot a `break` statement after each calculation.
Cases just serve as labels in C. After the code for one case is done,
execution falls through to the next. So, in your case it is always the
last statement (loesung = wert1 / wert2;) that is executed.
You should have something like
switch([list indexOfSelectedItem]) {
case 0:
loesung = wert1 + wert2;
break;
case 1:
loesung = wert1 - wert2;
break;
case 2:
loesung = wert1 * wert2;
break;
case 3:
loesung = wert1 / wert2;
break;
}
The last break is not strictly necessary. Please note, that it might be
useful to have a `default` clause.
Hope this helps
--Andreas