简体   繁体   English

它两次打印printf语句

[英]It prints The printf statement twice

This is really strange it prints this line printf("Do you want to continue Yes (Y) or No (N): \\n"); 这真的很奇怪,它打印此行printf("Do you want to continue Yes (Y) or No (N): \\n"); Not using any loop nothing but still it prints that statement twice 不使用任何环路不过它仍然打印出两次声明

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;

}

The first time scanf("%c", &check); 第一次scanf("%c", &check); finds some garbage (that does not match y or n ) so your program can do another loop. 找到一些垃圾(与yn不匹配),以便您的程序可以执行另一个循环。

As someone else already noticed, that garbage might be the newline after the ohm input. 正如其他人已经注意到的那样,在ohm输入之后,垃圾可能是换行符。

Some ideas to solve the problem: 解决问题的一些想法:
http://www.velocityreviews.com/forums/t436518-get-rid-of-trailing-newline-for-scanf.html http://www.velocityreviews.com/forums/t436518-get-rid-of-trailing-newline-for-scanf.html

 scanf(" %c", &input); 

(leading space will eat whitespace in the input) (前导空格将在输入中占用空白)


scanf() leaves the new line char in the buffer scanf()将新行char留在缓冲区中

Use scanf(" %d", &led); 使用scanf(“%d”,&led); , scanf(" %c", &check); ,scanf(“%c”,&check); etc. in the code. 等在代码中。 Adding an extra space before the format specifier will solve the problems caused by garbage/newline in the buffer. 在格式说明符之前添加额外的空间将解决缓冲区中的垃圾/换行符引起的问题。

scanf("%d", &ohm);

Here when you input a number and press ENTER, the number is processed by scanf , but the newline is still in the input buffer, then later 在这里,当您输入数字并按ENTER键时,该数字将由scanf处理,但换行符仍在输入缓冲区中,然后在后面

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

check will actually store the newline, not 'N' , check实际上将存储换行符,而不是'N'

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

so flag will not be set to 1 , the while loop will continue again. 因此flag将不会设置为1while循环将再次继续。 In the second loop, check can be assigned 'N' , the loop will terminate after. 在第二个循环中,可以将check分配为'N' ,然后循环将终止。

Please try scanf("%c ", &check); 请尝试scanf("%c ", &check); . Notice the extra space given after %c which will absorb extra new line character. 请注意%c之后的多余空格,它将吸收额外的换行符。

使用getchar ,它可以正常工作,您可以在这里尝试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM