如何将“-”更改为刽子手(C)的字母?
Posted
技术标签:
【中文标题】如何将“-”更改为刽子手(C)的字母?【英文标题】:How to change "-" to a letter for hangman (C)? 【发布时间】:2020-09-26 04:08:11 【问题描述】:我正在开发一个刽子手游戏,我的单词隐藏在破折号后面,当玩家猜对正确的字母时,他们会转向单词。
int numdashes;
char word[NUM_LETTERS_MAX] = "Coding" ;
char dash[NUM_LETTERS_MAX] = "------";
char guess;
char str[7] = "Coding";
int numletters = 6;
int numguesses;
int count;
int loopcount;
int i;
int k;
int main()
printf("Your Word Is: %c", &dash);
//printf("\nWord: %c", &dash);
loopcount = 10;
numguesses = 1;
while (numguesses <= loopcount)
for (int i = 0; i < numletters; i++)
dash[i] = "-";
dash[numletters = '\0'];
printf("\nPick a letter to guess: ");
scanf_s(" %c", &guess);
numguesses = numguesses + 1;
for (int k = 0; k < numletters; k++)
if (guess == word[k])
dash[k] = guess;
printf("\nYou are at Guess Number %d, Your word is at %c", numguesses, word);
/*for (int i = 0; i < numletters; i++)
dash[word[i]]++;
*/
if (numguesses < 10)
printf("\n\n\nYou Won the Game, Your Awesome");
else (numguesses == 10);
printf("\n\n\nI'm Sorry, the word was %s", &word);
我遇到了几个问题。第一个 for 循环不显示我的破折号“------”。我的另一个问题是我的数组似乎不起作用,所以它没有扫描用户输入的单词。谢谢
【问题讨论】:
很多问题。建议您一次处理任务的一小部分。在移动到下一部分之前把它做好。对于初学者:printf("Your Word Is: %c", &dash);
应该是 printf("Your Word Is: %s", dash);
和 dash[i] = "-";
应该是 dash[i] = '-';
,
另外,我想你想在循环中打印dash
而不是word
。再次使用 %s
而不是 %c
字符串:printf("\nYou are at Guess Number %d, Your word is at %s", numguesses, dash);
请尝试minimal reproducible example。理想情况下,只是您的第一个问题。
【参考方案1】:
作为 C 程序员,首先要学习的是:
如何将编译器设置为高警告级别
要学习的第二件事是:
将所有警告视为错误
如果你这样做了,你就可以自己解决很多代码中的问题。
第一个问题是发布的代码甚至无法编译。 NUM_LETTERS_MAX
未知,这会导致编译错误。其次,代码末尾需要有一个额外的。再次,编译错误。
当这两个问题得到修复时,会/可能会给出以下警告:
prog.c:23:28: warning: format ‘%c’ expects argument of type ‘int’, but argument 2 has type ‘char (*)[10]’ [-Wformat=]
printf("Your Word Is: %c", &dash);
~^ ~~~~~
prog.c:32:21: warning: assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion]
dash[i] = "-";
^
prog.c:34:13: warning: value computed is not used [-Wunused-value]
dash[numletters = '\0'];
~~~~^~~~~~~~~~~~~~~~~~~
prog.c:47:64: warning: format ‘%c’ expects argument of type ‘int’, but argument 3 has type ‘char *’ [-Wformat=]
printf("\nYou are at Guess Number %d, Your word is at %c", numguesses, word);
~^ ~~~~
%s
prog.c:59:22: warning: statement with no effect [-Wunused-value]
else (numguesses == 10);
~~~~~~~~~~~~^~~~~~
prog.c:61:48: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘char (*)[10]’ [-Wformat=]
printf("\n\n\nI'm Sorry, the word was %s", &word);
~^ ~~~~~
prog.c:62:5: error: expected declaration or statement at end of input
^
prog.c:37:9: warning: ignoring return value of ‘scanf’, declared with attribute warn_unused_result [-Wunused-result]
scanf(" %c", &guess);
^~~~~~~~~~~~~~~~~~~~
在执行您的程序之前,必须修复这些警告
一个例子:
prog.c:23:28: warning: format ‘%c’ expects argument of type ‘int’, but argument 2 has type ‘char (*)[10]’ [-Wformat=]
printf("Your Word Is: %c", &dash);
这告诉您不能使用printf("Your Word Is: %c", &dash);
打印字符串。如果您不知道如何在 C 中打印字符串,则必须进行一些研究。在 C 书籍中查找或搜索网络。但是不要只是忽略警告。
要打印字符串,请使用:printf("Your Word Is: %s", dash);
现在继续下一个警告:
prog.c:32:21: warning: assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Wint-conversion]
dash[i] = "-";
这告诉您不能通过指定 "-"
来更改单个字符。再说一次——如果你不知道如何改变一个字符,研究那个特定的问题。但不要只是忽略警告。答案是:dash[i] = '-';
像这样继续 - 一个一个地删除警告。
一旦你有一个干净的编译(即没有错误,没有警告),那么你就可以开始查看程序的功能行为了。
【讨论】:
以上是关于如何将“-”更改为刽子手(C)的字母?的主要内容,如果未能解决你的问题,请参考以下文章