Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基本的C hangman代码问题_C - Fatal编程技术网

基本的C hangman代码问题

基本的C hangman代码问题,c,C,嘿,伙计们,我正在学习C语言,我开始编写一个刽子手游戏。这应该播放超过1个单词,我选择了“informatik” 我想做的是询问用户名,然后开始 #include<stdio.h> int main() { int correct = 0; int mistake = 0; int number = 0; char name[20]; char Word[]={'i','n','f','o','r','m','a','t','i','k'}; char gu

嘿,伙计们,我正在学习C语言,我开始编写一个刽子手游戏。这应该播放超过1个单词,我选择了“informatik”

我想做的是询问用户名,然后开始

#include<stdio.h>            
 int main() {
int correct = 0;
int mistake = 0;
int number = 0;
char name[20];
char Word[]={'i','n','f','o','r','m','a','t','i','k'};
char guessStatus[]={'_','_','_','_','_','_','_','_','_','_','\0'};
char guess;
printf("##### Welcome to Hangman #####\nWhat is your Name?\n");
scanf(" %s", name);
fflush(stdin);
printf("Hello %s!\n", name);                                                                               
   while (mistake <=10 && number<10) {
    printf("Guess a letter: ");
    guess = getchar();
    printf("%c", guess);
    correct = 0;
    for (int search=0; search<10; search++)
    {                                                


        if (guess == Word[search]){
            number++;
            guessStatus[search] = Word[search];
            correct = 1;
        }
    }
    if (correct == 0){
        mistake++;
        printf("%c is wrong!\nYour Status is: %s\nYou have %i tries left\n", guess,guessStatus,10-mistake);
    }
        else {
            printf("Good job %s!\nYour Status is: %s\nYou have %i tries left\n", name,guessStatus,10-mistake);
        }

}
#包括
int main(){
int correct=0;
int错误=0;
整数=0;
字符名[20];
字符字[]={'i','n','f','o','r','m','a','t','i','k'};
字符猜测状态[]={uu',uu',uu',uu',uu',uu',uu',uu',uu',uu',uu','\0'};
猜字符;
printf(“欢迎来到Hangman”\n你叫什么名字?”;
scanf(“%s”,名称);
fflush(stdin);
printf(“你好%s!\n”,名称);

虽然(错误尝试此方法,但它应该有效
使用

而不是

guess = getchar();   //remove this line
printf("%c",  guess); //remove this line also
添加gethcar();将可以读取\n:

#include <stdio.h>            
 int main() {
int correct = 0;
int mistake = 0;
int number = 0;
char name[20];
char Word[]={'i','n','f','o','r','m','a','t','i','k'};
char guessStatus[]={'_','_','_','_','_','_','_','_','_','_','\0'};
char guess;
printf("##### Welcome to Hangman #####\nWhat is your Name?\n");
scanf(" %s", name);
getchar(); // add this line
printf("Hello %s!\n", name);                                                                               
   while (mistake <=10 && number<10) {
    printf("Guess a letter: ");
    guess = getchar();
    printf("%c", guess);
    correct = 0;
    for (int search=0; search<10; search++)
    {                                                


        if (guess == Word[search]){
            number++;
            guessStatus[search] = Word[search];
            correct = 1;
        }
    }
    if (correct == 0){
        mistake++;
        printf("%c is wrong!\nYour Status is: %s\nYou have %i tries left\n", guess,guessStatus,10-mistake);
    }
        else {
            printf("Good job %s!\nYour Status is: %s\nYou have %i tries left\n", name,guessStatus,10-mistake);
        }
}
}
#包括
int main(){
int correct=0;
int错误=0;
整数=0;
字符名[20];
字符字[]={'i','n','f','o','r','m','a','t','i','k'};
字符猜测状态[]={uu',uu',uu',uu',uu',uu',uu',uu',uu',uu',uu','\0'};
猜字符;
printf(“欢迎来到Hangman”\n你叫什么名字?”;
scanf(“%s”,名称);
getchar();//添加此行
printf(“你好%s!\n”,名称);

while(error)“it[…]不起作用”是什么意思?它崩溃了吗?您收到错误消息了吗?什么错误?
fflush(stdin)
是未定义的行为如果您不想“enter”算作字母,那么添加
if(guess='\n')continue;
或类似的内容。但最好检查字母:
if(guess<'a'| guess>'z')
,但详细信息将随您的区域设置和词汇而变化。它没有崩溃。它实际上可以工作,但工作错误。当我输入任何正确的单词,如“i”时。首先,它将“i”计算为正确,并将其置于猜测状态,错误编号不会更改。然后(不再输入任何字母)它将计算为“Enter”键作为一个字母,它是错误的。yes有效:)我也必须把它放在while语句中(在guess=getchar()和printf(“%c”,guess);。但是为什么?用户不应该用“enter”键输入他的单词?Googel“scanf line buffer”会知道的。
#include <stdio.h>            
 int main() {
int correct = 0;
int mistake = 0;
int number = 0;
char name[20];
char Word[]={'i','n','f','o','r','m','a','t','i','k'};
char guessStatus[]={'_','_','_','_','_','_','_','_','_','_','\0'};
char guess;
printf("##### Welcome to Hangman #####\nWhat is your Name?\n");
scanf(" %s", name);
getchar(); // add this line
printf("Hello %s!\n", name);                                                                               
   while (mistake <=10 && number<10) {
    printf("Guess a letter: ");
    guess = getchar();
    printf("%c", guess);
    correct = 0;
    for (int search=0; search<10; search++)
    {                                                


        if (guess == Word[search]){
            number++;
            guessStatus[search] = Word[search];
            correct = 1;
        }
    }
    if (correct == 0){
        mistake++;
        printf("%c is wrong!\nYour Status is: %s\nYou have %i tries left\n", guess,guessStatus,10-mistake);
    }
        else {
            printf("Good job %s!\nYour Status is: %s\nYou have %i tries left\n", name,guessStatus,10-mistake);
        }
}
}