Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 1, 2009, 10:45 PM   #1
Frankymac
macrumors newbie
 
Join Date: Nov 2009
Learning objective c on the mac

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");  Here I get "yesNo undeclared"
	}

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

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

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

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

Last edited by kainjow; Nov 8, 2009 at 02:15 AM. Reason: code tags
Frankymac is offline   0 Reply With Quote
Old Nov 1, 2009, 10:56 PM   #2
Cromulent
macrumors 603
 
Cromulent's Avatar
 
Join Date: Oct 2006
Location: 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.
__________________
Neural Advance - Mac OS X, UNIX and Windows Development
Last.fm Profile | Extreme Metal Reviews
MP 4x 2.66Ghz Xeons / 6GB RAM / 640GB + 500GB + 750GB + 1TB HDDs / ATI Radeon 4870 / iPad 3
Cromulent is offline   0 Reply With Quote
Old Nov 3, 2009, 10:34 AM   #3
Frankymac
Thread Starter
macrumors newbie
 
Join Date: Nov 2009
Thanks cromulent

I followed your advice. Could you give any advice about the code? It's very frustrating. Thanks Frank
Frankymac is offline   0 Reply With Quote
Old Nov 3, 2009, 10:43 AM   #4
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: 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
lee1210 is offline   0 Reply With Quote
Old Nov 7, 2009, 10:04 PM   #5
Frankymac
Thread Starter
macrumors newbie
 
Join Date: Nov 2009
Thanks Lee

I'll give it a try.

Frank
Frankymac is offline   0 Reply With Quote
Old Nov 8, 2009, 02:03 AM   #6
dukeofism
macrumors member
 
Join Date: Jul 2009
Quote:
Originally Posted by Frankymac View Post
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!
__________________
Check out My Website
A Poor College Student Trying to create a Small Income From Google ads and Affiliate marketing
Check out My Review of Windows 7 (BOOOO)
dukeofism is offline   0 Reply With Quote
Old Nov 9, 2009, 09:51 PM   #7
Frankymac
Thread Starter
macrumors newbie
 
Join Date: Nov 2009
Thanks for the advice dukeofism.

Thanks.
Frankymac is offline   0 Reply With Quote
Old Nov 10, 2009, 01:29 AM   #8
MorphingDragon
Banned
 
Join Date: Mar 2009
Location: The World Inbetween
Send a message via Skype™ to MorphingDragon
Quote:
Originally Posted by Frankymac View Post
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");  Here I get "yesNo undeclared"
	}

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

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

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

return(0)
	;} //main  Expected declaration of input"
Please learn some code layout, helps a bunch when other try read code. Plus it can avoid syntax errors.

Last edited by MorphingDragon; Nov 10, 2009 at 01:39 AM.
MorphingDragon is offline   0 Reply With Quote
Old Mar 24, 2010, 09:30 AM   #9
naituw
macrumors newbie
 
Join Date: Mar 2010
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));
}
naituw is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Which book for learning Objective-C should I choose? BoxerRobban472 iPhone/iPad Programming 17 Aug 27, 2014 02:22 AM
Resolved: Learning objective-C? Curax iPhone/iPad Programming 4 Sep 7, 2013 12:28 PM
Book to start learning C-objective themis.b iPhone/iPad Programming 12 Mar 29, 2013 11:43 AM
I Need Help Learning Objective-c!! Woodshark69 Mac Programming 11 Dec 14, 2012 08:21 AM
Learning Objective C iPadProd iPhone/iPad Programming 4 Nov 18, 2012 06:37 PM

Forum Jump

All times are GMT -5. The time now is 10:55 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC