I write a small console program in objective-c. It need to use the scanf method to receive the number.When I enter a character, it will make a mistake.So I try to solve it,but it has entered a cycle of death! See the following code, to help me solve it, thank you very much!
#import <Foundation/Foundation.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
int num1 = 0;
NSLog(@"Please input number:");
while (!scanf("%d", &num1)) {
fflush(stdin);
NSLog(@"Input error,just input number:");
}
}
return 0;
}