'gets()'函数第二次访问它时不要求字符串

时间:2016-05-02 11:47:35

标签: c gets

我写了一个刽子手游戏。

问题是当我在游戏结束后再次使用菜单系统进入main时通过调用main函数再次转到main时,gets函数只是不要求我输入字符串再次,只是为字符串分配一个空白。

P.S。:我正在使用DevC ++。

#include<stdio.h>
#include<strings.h>

main()
{
    int c,w=0,i,q,j,y,o,wrong,correct=0;
    c=0;
    wrong=0;
    char a,us='_',space=' ',x,ch;
    correct=0;
    printf("               %c",201);
    for(i=0;i<=8;i++)
        printf("%c",205);
    printf("%c",187);
    printf("\n");
    printf("               %c Hangman %c\n",186,186);
    printf("               %c",200);
    for(i=0;i<=8;i++)
        printf("%c",205);
    printf("%c\n",188);
    //  printf("  O  \n /|\\ \n/ | \\ \n / \\ \n/   \\\n");
    printf("\n |----|\n |    O  \n |   /|\\ \n |  / | \\ \n |   / \\ \n_|_ /   \\\n");
    char name[100];
    printf("Enter the name of the Movie:");
    gets(name);
    //  scanf("%s",&name);
    printf("The Movie you entered was: ");
    puts(name);
    int len=0;
    len=strlen(name);
    //  printf("\n%d",len);
    char guess[len];
    printf("\nPress any key and pass it to the person who has to guess the movie:");
    getch();
    printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
    printf("\nWelcome!\nGuess the name of the movie:\n");
    //  printf("  O  \n /|\\ \n/ | \\ \n / \\ \n/   \\\n");
    printf(" |----|\n |    O  \n |   /|\\ \n |  / | \\ \n |   / \\ \n_|_ /   \\\n");
    for(q=0;q<len;q++)
    {
        y=name[q];
        if ( y=='a' || y=='A' || y=='e' || y=='E' || y=='i' || y=='I' || y=='o' || y=='O' || y=='u' || y=='u' )
        {
            //  printf("%c",name[q]);
            guess[q]=name[q];
            c=c+1;
        }
        else if(y==' ')
        {
            //  printf("%c",space);
            guess[q]=space;
            c=c+1;
        }
        else
        {
            //  printf("%c",us);
            guess[q]=us;
            c=c+1;
        }
    }
    for(i=0;i<len;i++)
        printf("%c",guess[i]);
    while(1)
    {
        printf("\nGuess a letter: \n");
        scanf(" %c",&x);
        //  printf("%c\n",x);
        w=0;
        for(i=0;i<len;i++)
        {
            if(x==name[i]-32||x==name[i]+32||x==name[i])
                guess[i]=x;
            else
                w=w+1;
            if(w==len)
                wrong=wrong+1;
        }
        //  printf("\nw=%d\nlen=%d\n",w,len);

        for(i=0;i<len;i++)
            printf("%c",guess[i]);
        correct=0;
        //      printf("  0  \n /|\\ \n/ | \\ \n / \\ \n/   \\\n");
        for(i=0;i<len;i++)
        {
            if(guess[i]==name[i])
                correct=correct+1;
            //  printf("\n%d",correct);
        }   
        if(correct==len)
        {
            printf("\nCongratualtions!!!\nYou've Won!!!");
            printf("\nEnter 'R' to Restart Or 'Q' to Quit:\n");
            o=getch();
            if(o=='R'||o=='r')//Restart or quit menu
            {
                printf("\n");
                main();
            }
            else
                return 0;
        }
    //      printf("\n%d\n%d",correct,wrong);
        if(wrong==0)
            printf("\n  O  \n /|\\ \n/ | \\ \n / \\ \n/   \\\n");   
        if(wrong==1)
            printf("\n  O  \n /|\\ \n/ | \\ \n / \n/   \n");        
        if(wrong==2)
            printf("\n  O  \n /|\\ \n/ | \\ \n ");
        if(wrong==3)
            printf("\n  O  \n /| \n/ | \n");
        if(wrong==4)
            printf("\n O  \n | \n |" );
        if(wrong==5)
            printf("\n  O ");
        if(wrong==6)
        {
            printf("Oops You Ran Out Of Turns\n==========\nGAME OVER!!!\n==========\nThe Movie Was:\n");
            for(i=1;i<len;i++)
                printf("%c",name[i]);
            printf("\nEnter 'R' to Restart Or 'Q' to Quit:\n");
            o=getch();
            if(o=='R'||o=='r')//Restart or quit menu
            {
                printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
                main();
            }
            else
                return 0;
        }
        printf("------------------------------------------");
    }
}

enter image description here

1 个答案:

答案 0 :(得分:-2)

尝试在调用main()

之前输入此代码
fflush(stdin);