1

这真的很奇怪它打印这一行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;

}
4

5 回答 5

3

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

于 2013-07-29T07:07:56.453 回答
3

第一次scanf("%c", &check);发现一些垃圾(不匹配yor n),因此您的程序可以执行另一个循环。

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

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

scanf(" %c", &input);

(前导空格会吃掉输入中的空格)


scanf() 将新行字符留在缓冲区中

于 2013-07-29T06:53:03.810 回答
0
scanf("%d", &ohm);

这里当你输入一个数字并回车时,数字被 处理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',之后循环将终止。

于 2013-07-29T06:55:07.257 回答
0

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

于 2013-07-29T06:55:34.927 回答
0

使用它,它工作得很好,你可以在这里getchar试试

于 2013-07-29T06:56:12.900 回答