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

Janglo

macrumors member
Original poster
May 7, 2008
47
0
I am trying to do the "XYPoint" class exercise from Programming in Objective-C 2.0. I have my class, XYPoint with 2 float members (x and y). Methods are to set x and y and to get x and y. There is also another to exit the program. My two functions are "menu" and "getInput'. These go round on a loop in main. The user is presented with a menu with the 5 options and the getInput function runs through which number they picked and thus performs whatever action should be taken.

My problem comes that when I make a new instance of the XYPoint class at the beginning of main, I get errors when trying to use the instance methods.

XYPoint *mycoord = [XYPoint new];

When I call, for instance, [mycoord setx:input]; it said that "mycoord is undeclared". Here is my code - I don't have a clue what's going wrong :confused:

Code:
#import <Foundation/Foundation.h>

@interface XYPoint: NSObject
{
	float x;
	float y;
}

-(void) setx : (float) argx;
-(void) sety : (float) argy;
-(float) x;
-(float) y;

@end

@implementation XYPoint

-(void) setx : (float) argx
{
	x = argx;
}

-(void) sety : (float) argy
{
	y = argy;
}

-(float) x
{
	return(x);
}

-(float) y
{
	return(y);
}

@end


void menu(void)
{
	NSLog(@"\n\n----------\n\
		 1 - setx\n\
		 2 - sety\n\
		 3 - getx\n\
		 4 - get y\n\
		 5 - exit\n\
		 \n----------\n\n"); 
}

void getInput(void)
{
	char option;
	
	option = getchar();
	
	if(option == '1')
	{
		float input;
		
		NSLog(@"\n\nSet x to: ");
		scanf("%f",&input);
		getchar();
	
		[mycoord setx:input];
	}
	
	else if(option == '2')
	{
		float input;
		
		NSLog(@"\n\nSet y to: ");
		scanf("%f",&input);
		getchar();
		
		[mycoord sety:input];
	}
	
	else if(option == '3')
	{
		NSLog(@"x = %f",[mycoord x]);
		getchar();
	}
	
	else if(option == '4')
	{
		NSLog(@"y = %f",[mycoord y]);
		getchar();
	}
	
	else if(option == '5')
		break;
	
}

int main (int argc, const char * argv[]) 
{
	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	XYPoint *mycoord = [XYPoint new];
	
	while(1)
	{
		menu();
		getInput();
	}
	
	[pool drain];
	return 0;
}
 

robj

macrumors regular
Dec 23, 2009
230
0
Madrid, Spain
You are declaring mycord inside main function, so it's only visible within main.

As you are using it from antoher different function getInput, you have to pass the reference of this instance to this method.

For example your getInput function could look like:
Code:
void getInput(XYPoint* mycoord)

And you have to pass your local variable to the function
Code:
XYPoint *mycoord = [XYPoint new];
...
getInput(mycoord);
 

Janglo

macrumors member
Original poster
May 7, 2008
47
0
Ah brilliant, it worked!

Thanks very much =D

(Is there a way I can use the break statement to break out of the loop in option 5. It kept bringing up an error saying that it couldn't use break because it wasn't in a loop?)
 

chown33

Moderator
Staff member
Aug 9, 2009
10,739
8,415
A sea of green
(Is there a way I can use the break statement to break out of the loop in option 5. It kept bringing up an error saying that it couldn't use break because it wasn't in a loop?)
No, you can't use break from another function.

Consider these two facts:
1. A while loop will continue as long as a condition is non-zero.
2. A function can return a value that is zero or non-zero.

So look at your while(1) statement and think about ways to use those two facts (one or both) to stop the loop.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.