循环正在跳过C中的第二个输入

时间:2018-10-11 22:32:03

标签: c arrays string

我试图用一些字符填充一个简单的数组,我的循环看起来像这样

for(i = 0; i <= 6; i++) {
    printf("Enter each value of the plate number:");
    scanf("%c", &arr[i]);
    printf("i = %d\n", i);
}

执行循环时,会发生这种情况,

Enter each value of the plate number:A
i = 0
Enter each value of the plate number:i = 1
Enter each value of the plate number:B
i = 2
Enter each value of the plate number:i = 3
Enter each value of the plate number:C
i = 4
Enter each value of the plate number:i = 5
Enter each value of the plate number:D
i = 6

我不知道为什么我每隔第二次机会就跳过一次以获得用户输入。

1 个答案:

答案 0 :(得分:0)

尝试一下 scanf(" %c", &arr[i]);(%c之前的空格)

scanf("%c\n", &arr[i]);

您有此问题,因为scanf将enter保留在输入缓冲区中。