【发布时间】:2014-11-25 11:09:20
【问题描述】:
我在编写程序时遇到了一些问题。该程序本质上是猜词游戏刽子手。我有一个词,用户有很多机会猜到这个词。我无法让游戏正确循环并在猜测次数用完时结束。另外,我创建了第二个数组,其大小与我的单词数组相同,但这个数组填充了破折号。如果一个字母猜对了,我需要正确的字母出现在破折号周围。如果有人可以提供帮助,我将不胜感激。
#include <stdio.h> // Input and output operations
#include <stdlib.h> // General purpose functions
int main(void)
{
FILE*fp; // File pointer variable
fp = fopen("Quiz 6", "w");
int numTries, tries, i; // Declaration of variables
char guess;
char myWord [6] = {'a','p','p','l','e','\0'}; // Initialization of variables
char dashedArray [6] = {'-','-','-','-','-','-'};
numTries = 0;
i=0;
tries = 0;
printf("\nLet's play a game! The objective of the game is to guess my secret word. You will have
five chances to guess.\n");
printf("\nLet's get started!\n");
printf("\nPlease enter a letter: ");
scanf("%c", &guess);
for (i = 0; i < 4; i++)
{
numTries = numTries +1;
if (guess == myWord [0])
{
printf("\n %c----", myWord [0]);
printf("\nGood Guess!\n");
printf("\nPlease guess another letter: ");
scanf(" %c", &guess);
}
if ( guess == myWord [1] && myWord [1] )
{
printf("\n-%c%c--", myWord [1], myWord [1]);
printf("\nGood Guess!\n");
printf("\nPlease guess another letter: ");
scanf(" %c", &guess);
}
if ( guess == myWord [3] )
{
printf("\n---%c-", myWord [3]);
printf("\nGood Guess!\n");
printf("\nPlease guess another letter: ");
scanf(" %c", &guess);
}
if ( guess == myWord [4] )
{
printf("\n----%c", myWord [4]);
printf("\nGood Guess!\n");
printf("\nPlease guess another letter: ");
scanf(" %c", &guess);
}
if ( if word is completed and guessed correctly )
{
printf("\nCongrats! You guessed the secret word!\n");
}
else if ( guess != myWord )
{
printf("\nSorry. That is not a correct letter. Please guess again:\n");
scanf(" %c", &guess);
}
}
}
【问题讨论】:
-
你忘了问问题。您需要什么帮助?
-
是的,此页面上唯一的问号 (?) 是 @DavidSchwartz 的 ...
-
@DavidSchwartz:我认为他向他的代码寻求帮助。他提到的工作不正常
-
他可能想查看 ncurses 库以获得一些帮助,如果您猜对了,那么如果您不知道它是什么,请阅读 man invisible-island.net/ncurses/man/ncurses.3x.html。
-
@ralph 对,但是什么有帮助吗?他需要帮助修复编译器错误吗?他需要帮助找出要使用的算法吗?他的代码做错了吗?如果是这样,他希望它做什么?他在观察什么?
标签: c