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
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
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;
}