views:

91

answers:

2

I'm working on the book "programming in objective c 2.0" and im not understanding why this program is not working. basically i need to build a program to convert a fahrenheit value to a celcius one.

I figured to just solve it very simply without objects and just use a straight procedural methodology, any way the problem I'm having is that the values of the variables I define to represent the fahrenheit or celcius values are coming up kind of random.

Here's my code:

#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    float  fahrenheit;
    float  celciusConverted;
    fahrenheit = 27.0;
    celciusConverted = ( fahrenheit - 32 ) / 1.8 ;
    NSLog(@"%f degrees fahrenheit is equal to %f degrees celcius") , fahrenheit, celciusConverted;
    [pool drain];
    return 0;
}
+5  A: 

The closing parenthesis in your NSLog statement is misplaced. It should be just before ;

What you have is

NSLog(@"... %f %f ..."), arg1, arg2;

The compiler doesn't seem smart enough to see that none of the %f have a corresponding argument, a common pitfall with variadic functions like NSLog(). After the closing parenthesis, the comma operator kicks in and the expressions arg1 and arg2 do nothing.

sigjuice
The comma operator should just be chucked in the next version of C. Its only purpose is to make obvious mistakes like this still be syntactically valid.
Chuck
Thanks so much, works great, this place rocks, will definitely be using this site a lot because I'm just starting out coding with objective c, have a very limited knowledge of programming but have my heart set on creating an iphone app (take a number right) anyway really appreciate the help.Nick
nickthedude
+3  A: 

The arguments to a function go inside the parentheses.

NSLog(@"%f degrees fahrenheit is equal to %f degrees celcius", fahrenheit, celciusConverted);
Chuck
thanks! that worked, appreciate the help, just starting out here.
nickthedude