簡體   English   中英

它兩次打印printf語句

[英]It prints The printf statement twice

這真的很奇怪,它打印此行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("%c", &check); 找到一些垃圾(與yn不匹配),以便您的程序可以執行另一個循環。

正如其他人已經注意到的那樣,在ohm輸入之后,垃圾可能是換行符。

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

 scanf(" %c", &input); 

(前導空格將在輸入中占用空白)


scanf()將新行char留在緩沖區中

使用scanf(“%d”,&led); ,scanf(“%c”,&check); 等在代碼中。 在格式說明符之前添加額外的空間將解決緩沖區中的垃圾/換行符引起的問題。

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' ,然后循環將終止。

請嘗試scanf("%c ", &check); 請注意%c之后的多余空格,它將吸收額外的換行符。

使用getchar ,它可以正常工作,您可以在這里嘗試

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM