Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 读取字符时的Scanf行为_Iphone_Objective C - Fatal编程技术网

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