c - fgets 不通过 while 循环 C 进行第二次扫描

标签 c loops while-loop scanf fgets

所以我制作了这个程序来扫描一个地址,然后询问用户是否想要插入另一个地址或打印已经插入的地址。当我运行它时,我会浏览一次,然后我为 &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/

相关文章:

c - `#ifdef MODULE` 围绕 module_exit() 的用途?

c - 为匿名函数创建一个 C 语言插件是否实用?

c# - 更快的循环方法 ('for' 和 'foreach' )?

python - 你如何在循环中找到 Python 数组中元素的索引

C编程: Generating a random number (x) that is between -2, 2然后更新x

c - 函数中的变量初始化

在 c 中的 main() 中调用 main()

c - 减少双循环中的迭代次数

Java最大函数递归

java - do while 循环后无法继续执行 main 方法