我是C编程新手。它说我没有初始化myLetter而且我一直收到运行时错误
这是我的代码:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
// MAXWORD, which will be the max word length
#define MAXWORD 20
// INCORRECT_GUESSES, which will be the max guesses
#define INCORRECT_GUESSES 6
/* 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;
}
fscanf( word, "%s", theWord );
printf( "%s\n", theWord );
lower_string( theWord );
printf( "%s\n", theWord );
play( 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( "SORRY! your letter is not in the word\n" );
return 0;
}
else if (num>0)
{
printf( "Congratz! your letter was in the word\n" );
printf("%s", 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( "%c", soFar );
for( i=0; i<INCORRECT_GUESSES; i++ )
{
get_letter( theWord, soFar );
if( get_letter == 0)
{
printf( "Sorry, you're out of guesses" );
}
else
{
printf( "You win");
}
}
}
答案 0 :(得分:3)
scanf( " %c", theLetter );
应该是
scanf( " %c", &theLetter );
了解数组和指针的时间。如果你是C程序员,不能把它关掉很长时间。
答案 1 :(得分:2)
问题在于如何使用scanf()。请使用此[link]查看一些详细信息。
如果你重申: scanf(&#34;%c&#34;,theLetter) 有: scanf(&#34;%c&#34;,&amp; theLetter) 一切都会好的。
完整工作守则:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
// MAXWORD, which will be the max word length
#define MAXWORD 20
// INCORRECT_GUESSES, which will be the max guesses
#define INCORRECT_GUESSES 6
/* 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;
}
fscanf( word, "%s", theWord );
printf( "%s\n", theWord );
lower_string( theWord );
printf( "%s\n", theWord );
play( 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( "SORRY! your letter is not in the word\n" );
return 0;
}
else if (num>0)
{
printf( "Congratz! your letter was in the word\n" );
printf("%s", 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( "%c", soFar );
for( i=0; i<INCORRECT_GUESSES; i++ )
{
get_letter( theWord, soFar );
if( get_letter == 0)
{
printf( "Sorry, you're out of guesses" );
}
else
{
printf( "You win");
}
}
}
答案 2 :(得分:0)
int get_letter( char *theWord, char *soFar )
{
char theLetter;
printf("\nPlease enter a letter: ");
scanf( " %c", &theLetter ); // <--- rigth here
theLetter = tolower(theLetter);
letter_in_word( theWord, soFar, theLetter );
return theLetter;
}