void main()
{
int cnt=1;
char i;
while(cnt<4)
{
printf("\nenter the character");
scanf("%c",&i);
if(i>64 && i<91)
printf("\ncharacter is entered");
else
printf("\nnumber is entered");
cnt++;
}
}
在上面的程序中,在第二次迭代期间,i
自动取 10。所以控制转到else
部分。谁能帮我找出问题所在吗?
In the first iteration, you type a character and press Enter. scanf
consumes the character you entered, leaving the \n
in the standard input stream (stdin
).
在第二次迭代中,scanf
看到\n
字符消耗它,因此不等待进一步的输入。
你可以告诉scanf
使用以下命令读取并丢弃下一个字符:
scanf("%c%*c", &i);
或者你可以告诉scanf
在读取字符并将其存储到之前读取并丢弃所有空白字符(如果有)i
通过使用:
scanf(" %c",&i);
/* ↑ Note the space */
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)