Reputation: 1417
Now, I know that this is a simple question for MacOS, but when I compile a code with 'arc4random % n' in it, I just get an error log in Terminal saying:
main.m:9: error: ‘arc4random’ undeclared (first use in this function)
main.m:9: error: (Each undeclared identifier is reported only once
main.m:9: error: for each function it appears in.)
and I use:
gcc `gnustep-config --objc-flags` -lgnustep-base main.m -o main
to compile it
and here's my code (if it helps) :
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int number, guess;
number = arc4random() % 101;
while (!guess == number) {
NSLog (@"Please guess a number between 1 and 100");
scanf ("%i", &guess);
if (guess < number) {
NSLog (@"Sorry, guessed too low!");
}
else if (guess > number) {
NSLog (@"Sorry, guessed too high!");
}
}
NSLog (@"You guessed correct!");
[pool drain];
return 0;
}
Upvotes: 1
Views: 598
Reputation: 108169
You may consider using clang
instead of gcc
Use
clang -fno-objc-arc main.m -framework Foundation -o main
Also I'd use arc4random_uniform(101)
instead of arc4random() % 101
, since the former is bias free.
Upvotes: 2
Reputation: 19030
A few things:
Your use of >> and <<, these are not valid comparison operators. This will compile, but not perform what you expect. You either need to use > (greater than), >= (greater than or equals), < (less than) or <= (less than or equals).
Your compile error is due to your use of arc4random. This is a function, but you've not used it as such. You need to change your line to
number = arc4random() % 101;
Not 100% sure on this, but %i in your scanf looks like it should be %d
Upvotes: 2