它打印两次printf语句

时间:2013-07-29 06:49:47

标签: 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;

}

5 个答案:

答案 0 :(得分:2)

第一次scanf("%c", &check);发现一些垃圾(与yn不匹配),因此您的程序可以再次循环。

正如其他人已经注意到的那样,垃圾可能是欧姆输入后的换行符。

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

scanf(" %c", &input);
     

(前导空格会在输入中占用空格)


scanf() leaves the new line char in the buffer

答案 1 :(得分:2)

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

答案 2 :(得分:0)

scanf("%d", &ohm);

此处输入数字并按ENTER键时,数字由scanf处理,但换行符仍在输入缓冲区中,稍后

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

check实际上会存储换行符,而不是'N'

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

因此flag不会设置为1while循环将再次继续。在第二个循环中,可以为check分配'N',循环将在之后终止。

答案 3 :(得分:0)

请尝试scanf("%c ", &check);。注意在%c之后给出的额外空格将吸收额外的新行字符。

答案 4 :(得分:0)

使用getchar并且效果很好,您可以尝试here