所以我制作了这个程序来扫描一个地址,然后询问用户是否想要插入另一个地址或打印已经插入的地址。当我运行它时,我会浏览一次,然后我为 &y 插入的内容会打印一个随机数,然后再次打印带有注释的行并扫描 &y。我觉得它第二次通过 do-while 循环跳过 fgets 函数。救命!
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main (void)
{
char address[10][100];
int x;
char y;
for(x=0;x<10;x++){
do {
printf("Enter address %d:", x + 1); //prints this second time through
fgets(address[x], 100, stdin); //doesnt scan for this second time through
printf("Do you want to print address's inserted thus far or continue?(p or c):"); // prints this second time through also.
scanf("%d" , &y);
if (y == "c") continue;
else
printf("%d" , &y);
break;
} while (strcmp(address[x], "\n") == 0);
}
return(0);
}
最佳答案
fgets()
保存输入的换行符,并添加 NULL 字符。
例如,尝试将值从 100 更改为 99,以腾出一些空间。
如果您的输入(包括换行符)超过 99 个字符,那么您需要通过读取虚拟字符来“刷新”输入,直到到达换行符。
关于c - fgets 不通过 while 循环 C 进行第二次扫描,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23000466/