gpt4 book ai didi

c - 为什么我的 for 循环没有在 C 中给出预期的输出?

转载 作者:太空宇宙 更新时间:2023-11-04 01:49:14 25 4
gpt4 key购买 nike

<分区>

我正在尝试向我在 for 循环中定义的数组中添加一个字母。每次用户输入内容时,我想将“counterSoFar”减 1,并显示它。但是,“counterSoFar”总是减 2 并跳过用户输入任何内容的机会,而不是将“counterSoFar”减 1。我已经尝试多次查看代码,但我仍然无法弄清楚为什么。请指教。

# include <stdio.h>
# include <ctype.h>
void userInput(char[]);

int main() {
printf("Player 1 please enter a word of up to 12 letters.\n");
char word[13];
scanf("%13s", word);
userInput(word);
}
void userInput(char word[])
{
int n = strlen(word);
for (int i = 0; i < n; i++)
{
word[i] = tolower(word[i]);
}

int checker;
for (int i = 0; i < n; i++)
{

if (isalpha(word[i]))
{
checker = 1;
}
else
{
checker = 0;
printf("Please enter a word without spaces and numbers!\n");
break;

}
}
if (checker == 1)
{
int counterSoFar = 8;
char letter[1];
printf("The word to guess is as follows: \n");
for (int i = 0; i < n; i++)
{
printf("_");
}
int maxTries = 7;
for (int guessCounter = 0; guessCounter < maxTries; guessCounter++)
{
printf("\n");
counterSoFar = counterSoFar - 1;
printf("You have %d tries left!\n\n", counterSoFar);

printf("Player 2 please enter only 1 letter!\n");
scanf("%c", &letter);
char array[13];
for (int c = 0; c < n; c++)
{
if (letter == word[c])
{
array[c] = word[c];
}
else
{
array[c] = '_';
}
}
printf("The current array is %c", array);

}


}


}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com