Ruben A. Santiago
Ruben A. Santiago

Reputation: 125

Xcode: lost connection with Error code -1

This seemingly trivial piece of code is copied from the textbook but after Xcode takes the input at the scanf(), the program exits with error code -1 and says "lost connection".

#import <Foundation/Foundation.h>

int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc]init];

int number;

NSLog(@"Type in your number: ");
scanf("%i", &number);

if (number < 0)
    number = -number;

NSLog(@"The absolute value is: %i", number);
[pool drain];
return 0;

}

Update

I have reinstalled the OS from scratch. 10.8.5 and Xcode 5 — still the same issue. Installed Xcode 4.6.3, same issue, just no error message, the program just exits.

I also wrote a completely new program in a new blank project. The issue again only exists when the scanf function is called.

Upvotes: 2

Views: 2416

Answers (1)

Ruben A. Santiago
Ruben A. Santiago

Reputation: 125

Since the program expects an integer value to be inputted, I was hitting the "Enter" key on my keyboard (on the number pad). The scanf method terminates user input after the "Return" key is pressed. Although they are both often referred to as "Enter" keys, they behave very differently in this context.

Also, if you would like to use the "Enter" key, you can add a forward slash to the end of the input and then press "Enter" which would return the same results as the "Return" key.

Silly me.

Upvotes: 8

Related Questions