【问题标题】:C language - operation replaces mutiple array elements instead of oneC语言 - 运算符替换多个数组元素而不是一个
【发布时间】:2018-04-21 00:26:27
【问题描述】:

我正在用 C 语言创建刽子手,但有一个问题我不能完全掌握。当用户正确猜出被猜词所具有的字母之一时,程序会将之前猜到的所有字母替换给刚刚输入的那个用户。这个问题的根源是什么?

#include<stdio.h>
#include <stdlib.h>

int main()
{
  srand(time(NULL));
  int x = 0, isCompleted, matchFound, numberOfTries = 7;
  char letterGuess[1];
  int randomIndex = rand()%14; 
  const char *wordArray[14];
  const char *guessedWord[10];
  const char *usedLetters[17];
  for (int k = 0; k < 10; k++) {
    guessedWord[k] = "_";
  }
  wordArray[0] = "butonierka";
  wordArray[1] = "centyfolia";
  wordArray[2] = "chiroplast";
  wordArray[3] = "cmentarzyk";
  wordArray[4] = "chrustniak";
  wordArray[5] = "budowniczy";
  wordArray[6] = "cholewkarz";
  wordArray[7] = "cornflakes";
  wordArray[8] = "brzydactwo";
  wordArray[9] = "germanofil";
  wordArray[10] = "lichtarzyk";
  wordArray[11] = "lutowniczy";
  wordArray[12] = "mikrocysta";
  wordArray[13] = "tryskawiec";

  const char *wordToGuess = wordArray[randomIndex];

  for(int i = 0; i < 10; i++) {
    printf(" %s ", guessedWord[i]);
  }

  printf("\n");

  while(numberOfTries != 0 && isCompleted != 10) {
    matchFound = 0;
    isCompleted = 0;
    printf("Please give a lowercase letter\n");
    printf("Left tries: %d\n", numberOfTries);
    scanf("%s", &letterGuess);
    for (int z = 0; z < 17; z++) {
      if (usedLetters[z] == letterGuess[0]) {
        matchFound = 1;
      }
    }
    if (letterGuess[0] >= 'a' && letterGuess[0] <= 'z' && matchFound == 0) {
      usedLetters[x] = letterGuess[0];
      x++;
      for (int j = 0; j < 10; j++) {
        if (letterGuess[0] == wordArray[randomIndex][j])
          guessedWord[j] = letterGuess;
          matchFound = 1;
        }
      }
      if (matchFound == 0) {
        numberOfTries--;
      }
      for(int z = 0; z < 10; z++) {
         printf(" %s ", guessedWord[z]);
      }
      printf("\n");
    } else {
      if (matchFound == 1) {
        printf("You've already given such letter!!\n");
      } else {
        printf("Wrong input, please try again!\n"); 
      }
    }
    for (int k = 0; k < 10; k++) { 
      if (guessedWord[k] != "_") {
       isCompleted++;
      }
    }
    if (isCompleted == 10) {
      printf("You have correctly guessed a word! Congrats!!\n");
    }
    printf("\n\n");
  }

  printf("The word was: %s\n", wordArray[randomIndex]);
  printf("Game over!!\n");
}

【问题讨论】:

  • 欢迎来到 Stack Overflow! 寻求调试帮助的问题(为什么这段代码不起作用?)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:如何创建minimal reproducible example
  • 注意:isCompleted 未初始化。第一次迭代时可能是 10。
  • 您的代码无法编译。请提供minimal reproducible example
  • 第一个while循环之前呢?
  • 你的程序太复杂了,你应该从头开始重新考虑清楚的算法,你有太多的循环,太多的变量......使用函数!修改你的概念。

标签: c arrays replace


【解决方案1】:

问题在于您存储的是letterGuess,而不是单个字符。所以每次letterGuess 更新一个新的猜测,所有对它的引用都会改变。另外,letterGuess 太短,没有给终止空字符留下空间。

最佳解决方案是将letterGuess 设为char(或int),而不是数组,并将guessedWord 设为char [] 而不是char *[]。没有理由对单个字符使用字符串。这将解决字符串共享问题。

【讨论】:

    猜你喜欢
    • 2011-04-07
    • 2014-04-15
    • 2022-01-27
    • 2015-11-09
    • 1970-01-01
    • 1970-01-01
    • 2012-11-13
    • 2010-11-22
    • 1970-01-01
    相关资源
    最近更新 更多