I'm learning Objective-C and currently there is a strange behaviour in an else-if constuct. I have two programs, one after another. Each of them, asks for a number (or character) in a console, and then, based on an input, shows according message. In a second program I use else if. Here is how they look like:
// Pr. 6.6
int number6, sign;
NSLog(@"Please type in a number: ");
scanf("%i", &number6);
if (number6 < 0) {
sign = -1;
} else if (number6 == 0) {
sign = 0;
} else {
sign = 1;
}
NSLog(@"Sign = %i", sign);
//Pr. 6.7
char character;
NSLog(@"Enter a single character: ");
scanf( "%c", &character);
if ((character >= 'a' && character <= 'z') || (character >= 'A' && character <= 'Z')) {
NSLog(@"It's an alphabetic character");
} else if (character >= '0' && character <= '9') {
NSLog(@"It's a digit");
} else {
NSLog(@"It's a special character");
NSLog(@"%c", character);
}
The problem is, when the first program is executed, the else statement of the second program automatically gets executed, too. In the console I can see that it prints the message "Enter a single character: ", but it doesn't wait for the character to be entered. However, when I delete the first program, the second one works as expected. If you know what is the reason of this problem, I would appreciate your help.
Aucun commentaire:
Enregistrer un commentaire