我的Do-While循环似乎没有循环?

时间:2013-10-06 22:20:22

标签: c loops do-while

请帮助,当我要求用户使用scanf和printf继续输入时,我似乎无法让do-while循环循环。它可能是wipe_buffer吗?我似乎无法在这方面做到正确,我只需要完成循环,这应该是主要功能。新编码和本网站请不要太苛刻。拜托,谢谢。

    #include <stdio.h>
    #include <stdlib.h>
void wipe_buffer(void);
int main(int argc, char* argv[])

{
    char play1;
    char play2;
    char cont;

    do{
        printf("Player one pick Rock, Paper, or Scissors\n");
        scanf(" %c", &play1);
        wipe_buffer();
        printf("Player two pick Rock, Paper, or Scissors\n");
        scanf(" %c", &play2);
        wipe_buffer();

        switch(play1)
        {
            case 'r':
                 if(play2 == 'p' || play2 == 'P')
            {
                printf("Paper Covers Rock\n");
                printf("Player two wins\n");
            }
                if(play2 == 's' || play2 == 'S')
                {
                    printf("Rock Breaks Scissors\n");
                    printf("Player one wins\n");
                }
                if(play2 == 'r' || play2 == 'R')
                {
                    printf("Draw, Nobody wins\n");
                }
            break;
            case 'R':
                if(play2 == 'p' || play2 == 'P')
            {
                printf("Paper Covers Rock\n");
                printf("Player two wins\n");
            }
                if(play2 == 's' || play2 == 'S')
                {
                    printf("Rock Breaks Scissors\n");
                    printf("Player one wins\n");
                }
                if(play2 == 'r' || play2 == 'R')
                {
                    printf("Draw, Nobody wins\n");
                }
            break;
            case 'P':
                 if(play2 == 'p' || play2 == 'P')
            {
                printf("Draw, Nobody wins\n");
            }
                if(play2 == 's' || play2 == 'S')
                {
                    printf("Scissors cuts Paper\n");
                    printf("Player two wins\n");
                }
                if(play2 == 'r' || play2 == 'R')
                {
                    printf("Paper covers rock\n");
                }
            break;
            case 'p':
                 if(play2 == 'p' || play2 == 'P')
            {
                printf("Draw, Nobody wins\n");
            }
                if(play2 == 's' || play2 == 'S')
                {
                    printf("Scissors cuts Paper\n");
                    printf("Player two wins\n");
                }
                if(play2 == 'r' || play2 == 'R')
                {
                    printf("Paper covers rock\n");
                }
            break;
            case 's':
                 if(play2 == 'p' || play2 == 'P')
            {
                printf("Scissors Cuts Paper\n");
                printf("Player one wins\n");
            }
                if(play2 == 's' || play2 == 'S')
                {
                    printf("Draw, Nobody wins\n");
                }
                if(play2 == 'r' || play2 == 'R')
                {
                    printf("Rock breaks Scissors\n");
                    printf("Player two wins\n");
                }
            break;
            case 'S':
                 if(play2 == 'p' || play2 == 'P')
            {
                printf("Scissors Cuts Paper\n");
                printf("Player one wins\n");
            }
                if(play2 == 's' || play2 == 'S')
                {
                    printf("Draw, Nobody wins\n");
                }
                if(play2 == 'r' || play2 == 'R')
                {
                    printf("Rock breaks Scissors\n");
                    printf("Player two wins\n");
                }
            break;
        }
    printf("Do you wish to continue?\n");
    scanf(" &c", &cont);
    wipe_buffer();
    }while(cont == 'y' || cont == 'Y');
}
void wipe_buffer(void)
{
    char t;
    scanf("%c", &t);
    while(t != '\n' )
    {
        scanf("%c", &t);
    }
    return;
}

3 个答案:

答案 0 :(得分:3)

在本节中:

    printf("Do you wish to continue?\n");
scanf(" &c", &cont);

"&c"应为"%c"

请注意,当我使用gcc编译它时,我收到一条警告,基本上解决了这个问题,所以请务必阅读警告。

答案 1 :(得分:2)

在这里:

printf("Do you wish to continue?\n");
    scanf(" &c", &cont);
    wipe_buffer();
    }while(cont == 'y' || cont == 'Y');

应该有:

scanf(" %c", &cont);

下次遇到问题时使用-Wall -pedantic选项进行编译(如果使用gcc)。 它很好地展示了问题所在,如果不是 - 它会将其缩小一点。

这就是你的案子所显示的内容:

test2.c:117:5: warning: too many arguments for format [-Wformat-extra-args]
test2.c:120:1: warning: control reaches end of non-void function [-Wreturn-type]

所以这也是一个很好的提醒,你的main()函数必须return一些价值。

另一个建议是通过此链接:http://www.gidnetwork.com/b-60.html 并按照步骤页面进行操作 简而言之 - 与stdin相比,scanfgetchar读取一个字符时,有一些关于时间和空间消耗的信息。
请考虑在您的代码中应用此建议。

答案 2 :(得分:0)

因此,一个非常快速的方法来查看正在发生的事情是在循环之前删除调试printf语句

    scanf(" &c", &cont);
    wipe_buffer();
    printf("Debug: [%c]\n", cont);
    }while(cont == 'y' || cont == 'Y');

然后在程序运行时查找printf

我认为发生的事情是每个scanf在处理之前都在寻找Enter键,因此wipe_buffer会在继续之前请求大量的Enter键击。尝试在没有wipe_buffer的情况下运行程序,您仍然需要在继续之前按Enter键。