0

请帮忙,当我要求用户输入继续使用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;
}
4

3 回答 3

3

在这个部分:

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

"&c"应该"%c"

请注意,当我使用 gcc 编译它时,我收到了一个基本上解决了这个确切问题的警告,因此请务必阅读您的警告。

于 2013-10-06T22:28:40.980 回答
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一定的价值。

另一个建议是通过此链接 rad: http: //www.gidnetwork.com/b-60.html 并按照步骤页面。
简而言之 - 有一些关于时间和空间消耗如何从stdinwithscanf中读取一个字符的信息getchar
考虑在您的代码中应用此建议。

于 2013-10-06T22:28:43.550 回答
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 才能继续。

于 2013-10-06T22:32:13.663 回答