繁体   English   中英

数组不退回用户输入的字母

[英]Array not giving back letters the user entered

 int read_word(char word[], int max_size_word) {
 {
        int c = 0, let_count = 0;
        printf("\nPlease enter your word: ");
        char input = toupper(getchar());
        for(c = 1; c < 7; c++) {
            if(input != '\n') {
                word[c] = input;   
                let_count++;
            } else if(input == '\n')
              input = toupper(getchar());       //The word the user entered is in word[c]
            }
            return let_count;
         }
    }
    int check_word(char word[], int size_word, int letter_set[], int 
    size_letter_set, int arr[]) 
    {
        char word_copy;
        for(int ii = 0; ii < 7; ii++) {
            word_copy = word[ii];
        }
        printf("The word is %c\n" , word_copy);
    return 0;
}

我正在编写一个拼字游戏。 这是与我的问题有关的两个功能。 基本上我想检查我的读词功能是否正常。 那就是底部的printf所做的。 但是,当我输入几个字母时,我的“单词是...。” printf只返回输入的第一个字母。 我希望printf退回所有输入的字母。 任何帮助,将不胜感激!

您只打印一个字母,因为在printf("The word is %c\\n" , word_copy); 您的word_copy是一个char而不是一个string
在check_word中,尝试替换

char word_copy;
        for(int ii = 0; ii < 7; ii++) {
            word_copy = word[ii];
        }
        printf("The word is %c\n" , word_copy);
    return 0;

通过

int word_size = strlen(word); //calculate the length of your word
char word_copy[word_size + 1]; //create copy string with word size +1 for \0
int ii = 0;
        for(ii; ii < 7; ii++) { //I set ii < 7 to do like you but... Why did you set 7?
            word_copy[ii] = word[ii]; //put the characters of your word into the copy string
        }
    word_copy[ii] = '\0'; //end the string puttin a \0 at its end
    printf("The word is %s\n" , word_copy); //here i replace %c (char) by %s (string)
return 0;

read_word函数中存在类似的问题,如果您了解我在check_word函数中所做的修复,则应该能够修复它们(即使您可以将printf放入for循环中,我也认为这样做可以帮助您了解read_word中的问题)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM