gpt4 book ai didi

c - “gets()”函数在我第二次访问它时不询问字符串

转载 作者:行者123 更新时间:2023-11-30 18:15:15 25 4
gpt4 key购买 nike

我写了一个绞刑吏游戏。

问题是,当我在游戏结束后再次使用菜单系统进入 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

最佳答案

尝试将此代码放在 main() 调用之前

fflush(stdin);

关于c - “gets()”函数在我第二次访问它时不询问字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36981694/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com