Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Frankymac

macrumors newbie
Original poster
Nov 1, 2009
4
0
I've been reading this book and am having trouble with one of the sample codes that the author calls "bool party". I keep getting error when i try to "build and run". I've typed the code exactly as it appears in the book but i can't get it to work. Anyone familiar with this title? "learning objective c on the mac"

thank you

Code:
#import <Foundation/Foundation.h>

//returns NO if the two integers have the same
//value, YES otherwise

BOOL areIntsDifferent (int thing1, int thing2)
{
	if (thing1== thing2) {return(NO);}else {return (YES);}
}//areIntsDifferent

//given a NO value, return the human-readable
// strin "NO". Otherwise return "YES"

NSString *boolString (BOOL yesno)
{
	if (yesNo == NO) {return (@"NO");} else { return (@"YES");  [I]Here I get "yesNo undeclared"[/I]
	}

// boolstring
int main (int argc, const char *argv[])[I] here I get "nested functions are disabled use fnested functions[/I]
{
	BOOL areTheyDifferent; areTheyDifferent = areIntsDifferent (5, 5);
NSLog (@"are %d and %d different? %@", 5, 5, boolString(areTheyDifferent));

areTheyDifferent = areIntsdifferent (23, 42);  [I]Here I get "Implicit declaration of function"[/I]

NSLog (@"are %d and %d different? %@", 23, 42, boolString(areTheyDifferent));

return(0)
	;} //main  [I]Expected declaration of input"[/I]
 

Cromulent

macrumors 604
Oct 2, 2006
6,802
1,096
The Land of Hope and Glory
If you want help with any programming problem always post the relevant code and the exact error message or no one will be able to help you. Chances are you have missed a semi-colon somewhere.
 

Frankymac

macrumors newbie
Original poster
Nov 1, 2009
4
0
Thanks cromulent

I followed your advice. Could you give any advice about the code? It's very frustrating. Thanks Frank
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
C, and Objective-C, are case-sensitive. yesNo is not the same variable as yesno. areIntsdifferent is a different function than areIntsDifferent.

Also, you missed a closing } in this code:
Code:
NSString *boolString (BOOL yesno)
{
if (yesNo == NO) {return (@"NO");} else { return (@"YES"); Here I get "yesNo undeclared"
}

Change this to:
Code:
NSString *boolString (BOOL yesNo) {
  if (yesNo == NO) {
    return (@"NO");
  } else {
    return (@"YES");
  }
}
or
Code:
NSString *boolString (BOOL yesNo) {
  return yesNo==YES?@"YES":@"NO";
}

This should fix the nested function business. It things you're declaring main inside of boolString.

-Lee
 

dukeofism

macrumors member
Jul 22, 2009
55
0
I'll give it a try.

Frank

Just a general tip: (this may be because you didn't post your code in the code brackets) Your code is shown on this forums as all starting at the beginning of each line with no indentation. Including indentation for code that falls under a function or within a loop/ if-selector makes your code much more readable. Readability is something that you want your code to have because an increase in code readability makes it easier for you and other to read which makes for easier code to debug. Good luck with learning Objective-C!
 

MorphingDragon

macrumors 603
Mar 27, 2009
5,160
6
The World Inbetween
I've been reading this book and am having trouble with one of the sample codes that the author calls "bool party". I keep getting error when i try to "build and run". I've typed the code exactly as it appears in the book but i can't get it to work. Anyone familiar with this title? "learning objective c on the mac"

thank you

Code:
#import <Foundation/Foundation.h>

//returns NO if the two integers have the same
//value, YES otherwise

BOOL areIntsDifferent (int thing1, int thing2)
{
	if (thing1== thing2) {return(NO);}else {return (YES);}
}//areIntsDifferent

//given a NO value, return the human-readable
// strin "NO". Otherwise return "YES"

NSString *boolString (BOOL yesno)
{
	if (yesNo == NO) {return (@"NO");} else { return (@"YES");  [I]Here I get "yesNo undeclared"[/I]
	}

// boolstring
int main (int argc, const char *argv[])[I] here I get "nested functions are disabled use fnested functions[/I]
{
	BOOL areTheyDifferent; areTheyDifferent = areIntsDifferent (5, 5);
NSLog (@"are %d and %d different? %@", 5, 5, boolString(areTheyDifferent));

areTheyDifferent = areIntsdifferent (23, 42);  [I]Here I get "Implicit declaration of function"[/I]

NSLog (@"are %d and %d different? %@", 23, 42, boolString(areTheyDifferent));

return(0)
	;} //main  [I]Expected declaration of input"[/I]

Please learn some code layout, helps a bunch when other try read code. Plus it can avoid syntax errors.
 

naituw

macrumors newbie
Mar 24, 2010
1
0
another way

I wrote this

Code:
#import <Foundation/Foundation.h>
NSString *areintsdifferent (int a,int b)
{int c;
	c=a-b;
if (c == 0) {
	return (@"NO");
}
else {
	return (@"YES");
}
}

int main (int argc, const char *argv[])
{int x,y;
	scanf("%d%d",&x,&y);
	NSLog(@"%@",areintsdifferent(x, y));
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.