- Joined
- Jun 6, 2008
- Messages
- 27
- Reaction score
- 0
- Points
- 1
- Your Mac's Specs
- Blackbook - 2.4 GHZ Core 2 Duo - 2 GB RAM - 250 GB HDD
I am just learning objective-c. I have stephen kochan's new book on pre-order (anyone know when that should be out?) and I am just building simple programs for practice.
I made a program that calculates the first 13 numbers of Fibonacci's sequence, but now I am trying to make the program calculate one further number in the sequence per button push and I can't figure out how to do it. Here is the code I have so far. The error I get is that the variables in the else statement are undefined. I know that variables are protected and they can't be accessed from different functions (I guess the else statement makes it a different function?) but how do I get around this problem?
edit - I found that if I initialize the variables in the @interface section it will compile but I get the warning "local declaration hides instance variable" and when you run the program the first button press gives you the first 13 numbers but every press after that gives a zero.
I made a program that calculates the first 13 numbers of Fibonacci's sequence, but now I am trying to make the program calculate one further number in the sequence per button push and I can't figure out how to do it. Here is the code I have so far. The error I get is that the variables in the else statement are undefined. I know that variables are protected and they can't be accessed from different functions (I guess the else statement makes it a different function?) but how do I get around this problem?
Code:
#import "control.h"
@implementation control
- (IBAction)generate:(id)sender
{
if (buttonPress != 1)
{
buttonPress = 1;
int a = 0;
int b = 1;
int c;
NSLog(@"%d", a); //print first
NSLog(@"%d", b); //print second
c = a + b;
NSLog(@"%d", c); //print third
int i;
for (i=0; i<=9; i++) //print #4-#13 of the sequence
{
a = b;
b = c;
c = a + b;
NSLog(@"%d", c);
}
}
else // print one additional number in the sequence per button press
{
a = b;
b = c;
c = a + b;
NSLog(@"%d", c);
}
}
@end
edit - I found that if I initialize the variables in the @interface section it will compile but I get the warning "local declaration hides instance variable" and when you run the program the first button press gives you the first 13 numbers but every press after that gives a zero.