Forums
New posts
Articles
Product Reviews
Policies
FAQ
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Menu
Log in
Register
Install the app
Install
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
Objective - c program help (source included)
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="JoshMcg" data-source="post: 771935" data-attributes="member: 57811"><p>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. </p><p></p><p>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?</p><p></p><p>[CODE]#import "control.h"</p><p></p><p>@implementation control</p><p>- (IBAction)generate:(id)sender </p><p>{</p><p> </p><p> </p><p> if (buttonPress != 1)</p><p> {</p><p> buttonPress = 1;</p><p> int a = 0;</p><p> int b = 1;</p><p> int c;</p><p></p><p> NSLog(@"%d", a); //print first </p><p> NSLog(@"%d", b); //print second</p><p> c = a + b;</p><p> NSLog(@"%d", c); //print third</p><p> </p><p> int i;</p><p> for (i=0; i<=9; i++) //print #4-#13 of the sequence</p><p> {</p><p> a = b;</p><p> b = c;</p><p> c = a + b;</p><p> NSLog(@"%d", c);</p><p></p><p> }</p><p> }</p><p> </p><p> else // print one additional number in the sequence per button press</p><p> {</p><p> a = b;</p><p> b = c;</p><p> c = a + b;</p><p> NSLog(@"%d", c);</p><p> }</p><p> </p><p>}</p><p></p><p>@end[/CODE]</p><p></p><p>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.</p></blockquote><p></p>
[QUOTE="JoshMcg, post: 771935, member: 57811"] 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? [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[/CODE] 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. [/QUOTE]
Verification
Post reply
Forums
macOS & iOS Developer Playground
macOS - Development and Darwin
Objective - c program help (source included)
Top