刽子手:C语言初学者
我似乎不明白为什么当用户猜到所有字母时,我的程序不会结束游戏。我知道问题出在play()函数中。关于play函数中的更改内容或导致此问题的原因,是否有任何指示刽子手:C语言初学者,c,C,我似乎不明白为什么当用户猜到所有字母时,我的程序不会结束游戏。我知道问题出在play()函数中。关于play函数中的更改内容或导致此问题的原因,是否有任何指示 #include <stdio.h> #include <string.h> #include <ctype.h> // MAXWORD, which will be the max word length #define MAXWORD 10 // INCORREC
#include <stdio.h>
#include <string.h>
#include <ctype.h>
// MAXWORD, which will be the max word length
#define MAXWORD 10
// INCORRECT_GUESSES, which will be the max guesses
#define INCORRECT_GUESSES 5
/* Prototypes */
// Fills theArray with howMany copies of theLetter
void fill_array( char *theArray, int howMany, char theLetter );
// Get char from player, checks the letter, shows progress so far
int get_letter( char *theWord, char *soFar );
// Check if letter is in word, updates progress so far
int letter_in_word( char *theWord, char *soFar, char theLetter );
// Convert the word to lowercase
void lower_string( char *someWord );
// Play one game
void play( char *theWord );
/* Function definitions */
int main( )
{
char theWord [ MAXWORD ];
FILE* word;
word = fopen( "guesswords.txt", "r" );
if ( word == NULL )
{
printf( "No input file found..........\n" );
return -1;
}
printf("Want to play a game?\n");
fscanf( word, "%s", theWord );
lower_string( theWord );
play( theWord );
fprintf( word, "%s", theWord);
fclose( word );
return 0;
}
// Get char from player, checks the letter, shows progress so far
int get_letter( char *theWord, char *soFar )
{
char theLetter;
printf("\nPlease enter a letter: ");
scanf( " %c", &theLetter );
theLetter = tolower(theLetter);
letter_in_word( theWord, soFar, theLetter );
return theLetter;
}
// Fills theArray with howMany copies of theLetter
void fill_array( char *theArray, int howMany, char theLetter )
{
int i;
for( i=0; i<howMany; i++ )
{
theArray[i]= theLetter;
*(theArray + i) = theLetter;
*theArray = theLetter;
}
theArray[howMany] = '\0';
}
// Check if letter is in word, updates progress so far
int letter_in_word( char *theWord, char *soFar, char theLetter )
{
int i;
int num=0;
int len = strlen(theWord);
for( i=0; i<len; i++)
{
if (theWord[i] == theLetter )
{
soFar[i] = theLetter;
num++;
}
}
if (num == 0)
{
printf( "\nSORRY! your letter is not in the word\n" );
printf("%s\n", soFar);
return 0;
}
else if (num>0)
{
printf( "\nCongratz! your letter was in the word\n" );
printf("%s\n", soFar);
return 1;
}
}
// Convert the word to lowercase
void lower_string( char *someWord )
{
int i, cha;
int len = strlen( someWord );
for( i=0; i<len; i++ )
{
cha = someWord[i];
cha = tolower(cha);
someWord[i] = cha;
}
}
// Play one game
void play( char *theWord )
{
int i;
int len = strlen(theWord);
int guess = INCORRECT_GUESSES;
int result = 0;
char soFar[MAXWORD];
fill_array( soFar, len, '*');
printf( "Guess this word: %s\n", soFar );
for( i=0; i<INCORRECT_GUESSES; i++ )
{
guess = INCORRECT_GUESSES - (i+1);
get_letter( theWord, soFar );
if(get_letter<0)
{
printf( "\nYou have %d guesses left\n", guess);
}
else
{
printf( "\nYou have guessed all the letters! You have won!" );
return;
}
if( i == INCORRECT_GUESSES-1)
{
printf( "\nSorry, you're out of guesses\nBetter luck next time!\n" );
}
}
}
#包括
#包括
#包括
//MAXWORD,它将是最大字长
#定义MAXWORD 10
//猜测不正确,这将是最大猜测
#定义不正确的猜测5
/*原型*/
//在阵列中填充字母的副本数
空填充数组(char*theArray,int-howMany,char-theLetter);
//从玩家那里获取字符,检查字母,显示到目前为止的进度
int get_字母(char*theWord,char*soFar);
//检查字母是否在word中,更新到目前为止的进度
单词中的int字母(char*theWord,char*soFar,chartheletter);
//将单词转换成小写
无效下_字符串(char*someWord);
//玩一局
无效播放(字符*单词);
/*函数定义*/
int main()
{
char theWord[MAXWORD];
文件*字;
word=fopen(“guesswords.txt”,“r”);
if(word==NULL)
{
printf(“未找到输入文件………\n”);
返回-1;
}
printf(“想玩游戏吗?\n”);
fscanf(单词,“%s”,单词);
下弦(单词);
玩(单词);
fprintf(单词,“%s”,单词);
fclose(word);
返回0;
}
//从玩家那里获取字符,检查字母,显示到目前为止的进度
int get_字母(char*theWord,char*soFar)
{
字母字母;
printf(“\n请输入一个字母:”);
scanf(“%c”及其后者);
theLetter=tolower(theLetter);
单词中的字母(单词,soFar,字母);
回信;
}
//在阵列中填充字母的副本数
空填充数组(字符*数组,整数多少,字符长度)
{
int i;
对于(i=0;i您在播放中肯定有问题:
get_letter( theWord, soFar );
if(get_letter<0)
{
printf( "\nYou have %d guesses left\n", guess);
}
但代码中肯定还有其他问题。我建议您尝试在程序运行时单步执行程序,查看值,看看代码是如何工作的(或不工作)。您似乎没有对函数的返回值做任何处理。例如:
letter_in_word( theWord, soFar, theLetter );
此函数返回一个整数值(0或1),但您没有将其赋值给任何对象,因此此信息将丢失
这里也一样:
get_letter( theWord, soFar );
启动调试器并逐步完成程序。打印变量的状态并确保它们符合您的期望。堆栈溢出不是调试器;当您有特定问题时,请在此处提问。您可以尝试各种调试方法,使用gdb
运行程序(我相信是gnu调试器),通过valgrind
运行它,和/或使用strace
举几个例子——像这样运行gdb./
只是为了澄清:get\u letter
只标识函数(或在某些情况下,指向函数的指针)。它具体不表示函数的返回值。如果要在以后记住返回值,请将其指定给变量,与使用strlen(…)
和fopen(…)
所做的操作相同。
get_letter( theWord, soFar );