C ';获取();函数在我第二次访问它时不请求字符串
我写了一个刽子手游戏 问题是,当我在游戏结束后使用菜单系统再次进入main,通过调用main函数再次进入main时,C ';获取();函数在我第二次访问它时不请求字符串,c,gets,C,Gets,我写了一个刽子手游戏 问题是,当我在游戏结束后使用菜单系统再次进入main,通过调用main函数再次进入main时,get函数只是不要求我再次输入字符串,而是为字符串分配一个空格 附言:我正在使用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='_',spac
get
函数只是不要求我再次输入字符串,而是为字符串分配一个空格
附言:我正在使用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("------------------------------------------");
}
}
#包括
#包括
main()
{
int c,w=0,i,q,j,y,o,错误,正确=0;
c=0;
错误=0;
字符a,us='',空格='',x,ch;
正确=0;
printf(“%c”,201);
对于(i=0;i尝试将此代码放在main()的调用之前)
在C语言中没有gets()
。现在没有了。而且很好地摆脱了。--请使用正确的缩进。请始终使用{}
指示循环开始和结束的位置,即使对于单行循环体也是如此。请更清楚地询问您的问题。请不要向我们转储完整的程序并要求我们修复它们。我很确定您的意思是编写
,而不是
。每行只声明一个变量。用空格填充运算符(即if(x==y)
而不是if(x==y)
,这对可读性有很大帮助。不要养成在仍在试验时使用getch()
和类似的非标准函数的习惯;在标准函数(如fgets()
)的基础上构建。顺便说一句,user3121023是正确的。请检查“when*scanf()无法按预期工作”部分总的来说,你需要考虑为什么你使用超过25年的编程方法。你的学习C的来源已经完全过时了,需要被替换。@先生,我想我应该对我的问题更清楚。我不希望把我的代码转储,并要求人们解决它,也许我出错了。我只是想,如果我展示我的代码,就可以更容易地发现我的具体问题。感谢您提供的有关缩进的信息,我肯定会遵循这些信息。我后来还发布了我没有添加任何注释来帮助更好地理解我的问题。请阅读有关gets()的源代码并将尽量避免。谢谢fflush
ingstdin
是未定义的行为。请参阅以下内容:
fflush(stdin);