## Hi I'm new!

Apprentice
Posts: 6
Joined: 2007.10
Post: #1
Hi I'm new! In all ways, I could say...
I'm trying to learn programming, but as you know it's not an easy task! So, despite all the good books and tutorials, some compiling or runtime errors often occur.
When that happens, I, isolated learner, fall in panic and frustration.
So I was wandering if there is a place for programming beginners to show their code and ask for suggestion or fixing. Maybe some forum or whatsoever!

For example I was making a try to build a little calculator terminal program, but running it I get a runtime error that make it crash. Is in obj-c. Here the .h code:

Code:
```/* *  Calculator.h *  Obj-C exercises * *  Created by Angelo D'Ambrosio on 04/02/08. *  Copyright 2008 __MyCompanyName__. All rights reserved. * */ #import <Foundation/Foundation.h> @interface Calculator : NSObject {     int x;     int y;     float result; } - (float) addition:(int) anIntx :(int) anInty; - (float) subtraction:(int) anIntx :(int) anInty; - (float) multiplication:(int) anIntx :(int) anInty; - (float) division:(int) anIntx :(int) anInty; @end // and here's the main: // //  untitled1.m //  Obj-C exercises // //  Created by Angelo D'Ambrosio on 30/01/08. //  Copyright 2008 __MyCompanyName__. All rights reserved. // #include <stdio.h> #import <Foundation/Foundation.h> #import <Calculator.h> @implementation Calculator - (float) addition:(int) anIntx :(int) anInty; {          x = anIntx;     y = anInty;     result = x + y;     return result; } - (float) subtraction:(int) anIntx :(int) anInty; {     x = anIntx;     y = anInty;     result = x - y;     return result; } - (float) multiplication:(int) anIntx :(int) anInty; {     x = anIntx;     y = anInty;     result = x * y;     return result; } - (float) division:(int) anIntx :(int) anInty; {     x = anIntx;     y = anInty;     result = x / y;     return result; } @end int main () {     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];          int x, y;     int i = 0;     char operator;     Calculator *calc;              printf("Scrivi l'operazione richiesta: "); //write the requested operator         scanf ("%d %c %d", &x, &operator, &y);                  if (y == 0 && operator == '/')             printf("\nNon contemplato in N\n"); //not possible         else             switch (operator) {                 case '+':                     printf("\n Il risultato Ã¨ %f\n", [calc addition:x :y]); /*the result is*/                     break;                 case '-':                     printf("\n Il risultato Ã¨ %f\n", [calc subtraction:x :y]);                     break;                 case '*':                     printf("\n Il risultato Ã¨ %f\n", [calc multiplication:x :y]);                     break;                 case '/':                     printf("\n Il risultato Ã¨ %f\n", [calc division:x :y]);                     break;                 default:                     NSLog(@"errore\n"); //error                     break;             }                       [pool release];     return 0; } //I've add comments with english version of the strings.```

The error I get is:
[Session started at 2008-02-09 12:44:41 +0100.]
Scrivi l'operazione richiesta: 5+6
2008-02-09 12:44:52.394 Obj-C exercises[2002] *** +[Calculator addition::]: selector not recognized
2008-02-09 12:44:52.394 Obj-C exercises[2002] *** Uncaught exception: <NSInvalidArgumentException> *** +[Calculator addition::]: selector not recognized

Thank you and excuse me for the long post.
Member
Posts: 114
Joined: 2005.03
Post: #2
You're not actually creating a Calculator object anywhere.
Replace
Code:
`Calculator *calc;`
with
Code:
`Calculator *calc = [[Calculator alloc] init];`
Code:
`[calc release];`
before [pool release];

By the way, the code you've written is really not very useful for it's purpose. You don't need x, y and result as instance variables, instead you could write your calculation methods as
Code:
```- (float) subtraction:(int) anIntx :(int) anInty; { return (float) anIntx - anInty; }```
(Notice the (float), I think an explicit typecast is always better than an implicit one) - unless you need to keep the last result and last operands around for some reason, of course, but I don't think you actually do.
Apprentice
Posts: 6
Joined: 2007.10
Post: #3
Oh, thanks.
But isn't the NSAutoreleasepool object sufficient for creating new objects? In some example I've seen around they use NSAutor... instead of manual activation and it works, what I'm missing? here's the example:

Code:
```/* #import <Cocoa/Cocoa.h> int main(int argc,char **argv) {     int i;     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];     for (i=1;i<5;i++) {         NSString *str2 = [NSString stringWithFormat:@"%d + %d = %d",i,i,i +i];                                   NSLog(@"The value of str1 is '%@'",str2);     }     [pool release];     return(0); }```

However now it works, thanks again!

PS: so I can continue on posting here my coding questions?
Moderator
Posts: 768
Joined: 2003.04
Post: #4
Sure, although for non game oriented programming iDevApps would be more appropriate.
Member
Posts: 283
Joined: 2006.05
Post: #5
The autorelease pool is for... releasing.

It could replace [calc release], but you'd need to have something like:

Code:
`Calculator *calc = [[[Calculator alloc] init] autorelease];`

(which is the same as)

Code:
```Calculator *calc = [[Calculator alloc] init]; [calc autorelease];```

The NSString in the example you posted is being created by a class "convenience" method, which does all the alloc init stuff for you. This also means it's "owned" by the class, meaning that it's the classâ€™s responsibility to release it. (In fact, this just means it's added to the autorelease pool when it's created).

There's a good section on this in the documentation, but I found it very hard to understand until I'd played with some code.
Apprentice
Posts: 6
Joined: 2007.10
Post: #6
Uhm yes, found the reference. Hoping I could understand. Thanks. I'll have to study a bit.

@PowerMacX: games are the next step. I'll try for now to bother only if I really don't know how to solve problems. If I reckon I'll need more help than what I though, I will migrate on IDevApp.
Thanks