扫描不采用输入字符

  • 本文关键字:字符 扫描 c
  • 更新时间 :
  • 英文 :


这是我的代码,它将计数作为输入,然后将该数量的城市作为输入。
我想打印从该字母开始的所有城市,但程序在将字符作为输入之前结束。但它不需要字符输入

#include <stdio.h>
void main()
{
char a[20][10];
char ch;
int i,n;
printf("Enter nos of citiesn");
scanf("%d",&n);
for ( i = 0; i < n ; i++)
{
    scanf("%s",a[i]);
}
printf("n");
printf("Enter 1st charactern");
scanf("%c", &ch);
for ( i = 0; i < n ; i++)
    if(ch==a[i][0])
        printf("%sn",a[i]);    

}

输出:

Enter nos of cities
3
asd
zxc
qw
Enter 1st character

使用 scanf(" %c", &ch);

来自上一个输入的输入将作为输入。因此,请在%c之前添加一个空格。

只需在第 15 行打印换行符后getchar();添加一行因为printf("n")用换行符为scanf提供信息。 getchar()在这里跳过(吃掉(换行符。

最新更新