Iphone 读取字符时的Scanf行为
我有这个密码Iphone 读取字符时的Scanf行为,iphone,objective-c,Iphone,Objective C,我有这个密码 int firstNum; int secondNum; char operator; NSLog(@"Please enter the first number"); scanf("%i", &firstNum); NSLog(@"Please enter the operator"); //Line 3 scanf("%c", &operator); NSLog(@"Please enter the second number"); scanf("%i", &
int firstNum;
int secondNum;
char operator;
NSLog(@"Please enter the first number");
scanf("%i", &firstNum);
NSLog(@"Please enter the operator"); //Line 3
scanf("%c", &operator);
NSLog(@"Please enter the second number");
scanf("%i", &secondNum);
当我运行它,系统到达第3行时,它不会等待用户输入字符值,而是立即执行下一行。怎么了?调用scanf(“%c”)
没有跳过空格(请参阅)
c匹配一系列宽度计数字符(默认为1);下一个
指针必须是指向char的指针,并且必须有足够的空间
对于所有字符(未添加终止NUL)。照常
前导空白的跳过被抑制。跳过空白
首先,在格式中使用显式空格
所以它在读取第一个整数后读取缓冲区中任何一行结束字符
要显式跳过空白,可以将其替换为:
scanf(" %c", &operand);
请注意,如果用户输入123+234
并在第一个提示下点击enter,则随后的两个scanf
s将不会等待任何输入,而是简单地处理+
和234
如果这是一个问题,请阅读整行内容,然后使用sscanf