C 语言刽子手游戏,如何将 scanf 与蒙面词链接并为每个错误输入设置计数器 inc?



>我正在制作一个刽子手游戏,我创建了一个 randf 来从一批单词中进行选择,并屏蔽了单词,以便猜测者猜测随机单词的字母。问题在于我不知道如何将两者联系起来。我已经做了循环,但没有实际连接它们,它将始终在计数器= 0时打印,因为我没有为何时

for(int counter; answer != word; counter++;)

但是后来我得到错误:

操作数类型不兼容("char" 和 "char(*)[200]")。

有什么解决办法吗?

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <time.h>
#include <string>
#define ARRAY_SIZE 10
int main()
{
//randomwordgenerator
char word[ARRAY_SIZE][200] = { "tiger", "lion", "elephant", "zebra", "horse", "camel", "deer", "crocodile", "rabbit", "cat" };

int x = 0;
srand(time(0));
x = rand() % ARRAY_SIZE;
system("pause");//will pause the rand function
//masking and unmasking word
char m = strlen(word[x]);//will count the number of letters of the random word
int mask[200]{};
for (int i = 0; i < m; ++i) //loop until all leters are masked
{
mask[i] = 0;
}
//Introduction
printf("Hey! Can you please save me? n");
printf(" On/|\ n/ \ n");
//Ask for answer
printf("nType a letter to guess the word and save me. The letter is case sensitive so please pick lower case or I might dien");
char answer;
scanf_s("%d", &answer);
//loop w counter
for (int counter = 0; counter++;) {
if (counter == 0)
{
printf("n");
}
else if (counter == 1)
{
printf("n=========");
}
else if (counter == 2)
{
printf("n+n|n|n|n|n|n=========");
}
else if (counter == 3)
{
printf("n+---+n|   |n|n|n|n|n=========");
}
else if (counter == 4)
{
printf("n+---+n|   |n|   On|n|n|n=========");
}
else if (counter == 5)
{
printf("n+---+n|   |n|   On|   |n|n|n=========");
}
else if (counter == 6)
{
printf("n+---+n|   |n|   On|   |n|  / \ n|n=========");
}
else if (counter == 7)
{
printf("n+---+n|   |n|   On|  /| n|  / \ n|n=========");
}
else if (counter == 8)
{
printf("n+---+n|   |n|   On|  /|\ n|  / \ n|n=========");
}
else if (counter == 9)
{
printf("nReally left me hanging there buddy");
return 0;
}
else 
{
printf("nThanks for saving me!");
}
return 0;
}
}

首先,我建议您组织代码。不要犹豫,将其拆分为不同的函数,并尝试强迫自己尊重某种风格规范(例如,尝试在函数的开头声明所有变量)。

然后尝试定期编译你的程序,并学会阅读和理解错误(如果你遇到一些错误)。这不会直接回答您的问题,但会有所帮助。

scanf函数将用户输入存储到您调用的变量中answer。但似乎您再也不会使用answer变量了。您提出的解决方案(for(int counter; answer != word; counter++))抛出错误,因为您无法将两个字符串与C中的运算符进行比较。为此,您必须使用类似strcmp()的函数 (https://man7.org/linux/man-pages/man3/strcmp.3.html)。

另外,不要忘记初始化for循环的counter

为了提高代码的可读性,您还可以尝试将末尾的 if/else if 语句替换为开关/case 块。 https://www.guru99.com/c-switch-case-statement.html#:~:text=What%20is%20Switch%20Statement%20in,that%20particular%20case%20is%20executed。

相关内容

最新更新