c - 它打印 printf 语句两次

标签 c

这真的很奇怪,它打印了这一行 printf("Do you Want to continue Yes (Y) or No (N):\n"); 不使用任何循环,但仍然打印该语句两次

int main()
{

int led=0;
int ohm=0;
char check;
int flag=0;

while (led < 1 || led > 3){
    printf("Enter the number of switch you want to close: \n\n");
    printf("  ********************     Press 1 for switch (LED) 1     ********************\n");
    printf("  ********************     Press 2 for switch (LED) 2     ********************\n");
    printf("  ********************     Press 3 for switch (LED) 3     ********************\n");

    printf("Switch: ");
    scanf("%d", &led);
}

printf("\n\n");
while (ohm < 1 || ohm > 3){
    printf("Enter the resistance of Rheostat: \n\n");
    printf("  ********************     Press 1 for 10 ohm resistance  ********************\n");
    printf("  ********************     Press 2 for 20 ohm resistance  ********************\n");
    printf("  ********************     Press 3 for 30 ohm resistance  ********************\n");

    printf("Resistance: ");
    scanf("%d", &ohm);
}


    while (flag == 0)
    {
        //LED-1
        if(led== 1 && ohm== 1 )
        {
            printf("LED-1 is blinking 2 times\n");
        }

        if(led== 1  && ohm== 2)
        {
            printf("LED-1 is blinking 4 times\n");
        }

        if(led== 1  && ohm== 3 )
        {
            printf("LED-1 is blinking 6 times\n");
        }

        //LED-2
        if(led== 2  && ohm== 1 )
        {
            printf("LED-2 is blinking 2 times\n");
        }

        if(led== 2  && ohm== 2 )
        {
            printf("LED-2 is blinking 4 times\n");
        }

        if(led == 2  && ohm == 3)
        {
            printf("LED-2 is blinking 6 times\n");
        }

        //LED-3
        if(led == 3  && ohm == 1 )
        {
            printf("LED-3 is blinking 2 times\n");
        }

        if(led == 3  && ohm == 2)
        {
            printf("LED-3 is blinking 4 times\n");
        }

        if(led == 3 && ohm == 3)
        {
            printf("LED-3 is blinking 6 times\n");
        }

        led = 0;
        ohm = 0;
        printf("Do you want to continue Yes (Y) or No (N): \n");
        scanf("%c", &check);

        if(check =='Y' || check =='y')
        {

            while (led < 1 || led > 3){
            printf("Enter the number of switch you want to close on: ");
            scanf("%d", &led);
            }

            while (ohm < 1 || ohm > 3){
            printf("Enter the resistance of Rheostat: ");
            scanf("%d", &ohm);
            }
        }

        if(check=='N' || check=='n')
        {
            printf("Thanks for using the program");
            flag = 1;
        }



    }
    return 0;

}

最佳答案

使用 scanf("%d", &led); , scanf("%c", &check);代码中的等等。 在格式说明符前添加额外的空格将解决缓冲区中垃圾/换行符引起的问题。

关于c - 它打印 printf 语句两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17917638/

相关文章:

在 C 中反转 char 数组的二进制值的最干净方法?

ruby-on-rails - Windows 8 64 位 ruby​​-debug-ide 安装 linecache19 由于 gcc -m64 标志错误 : sorry, 未实现:64 位模式未编译而失败

c - 如何读取、加密和覆盖文本文件

c - 比我使用的递归更好的方法(更好的时间复杂度)

c - C中bool和while(true)的区别

c++ - 为什么 C++ 中的非 POD 类型对 C 客户端是不透明的?

c - 使用逻辑运算符和前/后增量无法从此代码中获得预期输出

c - 如何在C中找到挂载的U盘大小?

c - ATLAS 多线程 BLAS 例程中的 valgrind "uninitialized value"警告是否为误报?

c - 如何在 C 中 memset 一个结构