有没有一种方法可以在不使用fgets的情况下修复scanf检测到的回车(scanf检测出的回车导致dowhile运行两次



我遇到了与这里的某个人相同的问题,检测到scanf的回车键,导致while循环循环2次。

简单的解决方法是在%c前面添加一个空间用于scanf,或者只使用fgets。然而,在我的代码中,它们都不起作用。

所以我正在做一个hangman游戏,玩家1输入的字符串中的字母将被屏蔽,为此,我使用了一个for循环,这样对于玩家1输入中的每个字母,player1Mask将连接一个下划线,从而";掩蔽";或者隐藏信件。

if player 1 inputs "cats", a for loop will loop through player 1's input and replace player1Word Mask with a hyphen. So player1WordMask is "----"

当播放器2使用scanf输入一个字母时,它将执行for循环,以便对于播放器1Input中的每个字母,如果播放器2的字母与该字母相同,它将替换"0";掩模";在playerMask中,它是一个连字符,带有player2Input的字母。

if player 2 enters 'a', playerMask will become '-a--'.

所有这些都将在一个do-while循环中完成,该循环检查玩家2的猜测是否用完,或者玩家2是否猜到了单词。

我的问题是scanf,它将检测导致新行的回车键,运行循环两次。我不能在播放器2中的%c输入前面放一个空格,因为它会破坏掩码和播放器1之间的检查功能,使游戏无休止地运行。

fgets将导致整个游戏在不输入任何内容的情况下运行,从而结束程序。那么,有没有一种方法可以防止scanf在不使用fgets的情况下两次边做边跑?

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define WORDLIMIT 12
#define NUMBER_OF_GUESSES 7
char player1Word[WORDLIMIT];
char player1WordMask[WORDLIMIT];
char player2Input;
int player2GuessCount=7;
int specialCharFlag = 0;
int isWordAllowed = 0;
int guessCorrectly = 0;
int gameWon = 1;
int main(void)
{
do //check if player 1 word is valid
{
// prompt and get the word
printf("Player 1, enter a word of no more than %d letters:n", WORDLIMIT);
fgets(player1Word,WORDLIMIT,stdin);
// Player 1 enters a word with upper case letters, the program should change them to lower case.
for(int i = 0; i<strlen(player1Word); i++)
{
player1Word[i] = tolower(player1Word[i]);
}
//special character flag
specialCharFlag = 0;
if (strlen(player1Word) > 12)
{
printf("Enter a word of no more than %d letters:n", WORDLIMIT);
specialCharFlag = 1;
}
for (int i = 0; i < strlen(player1Word); i++)
{
//If Player 1 enters a word with punctuation marks or digits, he or she should be asked to enter another word.
if (player1Word[i] == '!' || player1Word[i] == '@' ||
player1Word[i] == '#' || player1Word[i] == '$' ||
player1Word[i] == '%' || player1Word[i] == '^' ||
player1Word[i] == '&' || player1Word[i] == '*' ||
player1Word[i] == '(' || player1Word[i] == ')' ||
player1Word[i] == '-' || player1Word[i] == '{' ||
player1Word[i] == '}' || player1Word[i] == '[' ||
player1Word[i] == ']' || player1Word[i] == ':' ||
player1Word[i] == ';' || player1Word[i] == '"' ||
player1Word[i] == ''' || player1Word[i] == '<' ||
player1Word[i] == '>' || player1Word[i] == '.' ||
player1Word[i] == '/' || player1Word[i] == '?' ||
player1Word[i] == '~' || player1Word[i] == '`' ||
player1Word[i] == '_')
{
printf("Sorry, the word must contain only English letters.n");
specialCharFlag = 1;
}
}
if (specialCharFlag == 1)
{
isWordAllowed = 0;
}
else
{
isWordAllowed = 1;
}
}
while (isWordAllowed == 0);
//At the beginning of each round, the program will output a row of characters containing one underscore for every letter in the word to be guessed.
for(int i = 0; i<strlen(player1Word)-1; i++)
{
player1WordMask[i] = '_';
}
do
{
// Player 2 (who again has not been watching Player 1) will be asked to guess one letter at a time.
printf("Player 2 has so far guessed: %sn",player1WordMask);
printf("Player 2, you have %d guesses remaining. Enter your next guess:n",player2GuessCount);
scanf("%c",&player2Input);
player2Input = tolower(player2Input);
guessCorrectly = 0;
for (int i = 0; i < strlen(player1Word); i++)
{
//If Player 2 has previously guessed a letter that is in the word, the underscore will be replaced by that letter.
if (player1Word[i] == player2Input)
{
player1WordMask[i] = player2Input;
guessCorrectly = 1;
}
}
if(guessCorrectly == 0)
{
player2GuessCount -= 1;
}
gameWon = strcmp(player1Word,player1WordMask);
if(gameWon==0)
{
break;
}
}
while(player2GuessCount!=0);
if(gameWon == 0)
{
printf("Player 2 wins.");
}

}

因此,显然在scanf的%c前面为player2Input留出空间是正确的做法。

问题是player1Word。我不应该使用fgets,而应该使用scanf(" %s",&player1Word),它解决了包括单词屏蔽在内的所有问题。

相关内容

  • 没有找到相关文章

最新更新