我是C编程新手。它说我没有初始化myLetter,我不断收到一个运行时错误开始C.变量未被初始化
这里是我的代码:
#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");
}
}
}
C++是不是C.标记都之前,您应该三思而后行。 – crashmstr
找不到'myLetter' ?! – user1810087
嗨!欢迎来到StackOverflow。请发布确切的错误和行号。 – Stefan