2013-11-22 193 views
-3

我是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"); 
      } 
     } 
    } 
+0

C++是不是C.标记都之前,您应该三思而后行。 – crashmstr

+1

找不到'myLetter' ?! – user1810087

+0

嗨!欢迎来到StackOverflow。请发布确切的错误和行号。 – Stefan

回答

3
scanf(" %c", theLetter); 

应该

scanf(" %c", &theLetter); 

时间来了解数组和指针。如果你是一名C程序员,那么不能拖延很长时间。

+0

哇,谢谢。 – user3019821

+0

你可能还会提到:'printf(“%c”,soFar);''应该使用'%s'。 –

+0

没问题。要理解的是为什么'scanf(“%c”,&theLetter);''需要''''fscanf(word,“%s”,theWord);'不。 – john

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; 
} 
2

问题在于如何使用scanf()。请使用此[链接]查看一些细节。

如果你repace: 的scanf( “%C”,theLetter) 有: 的scanf( “%C”,& 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"); 
     } 
    } 
}