我必须创建一个创建猜字母游戏的程序。 一切都成功建立。但是,我遇到了一个问题,因为GetTheLetter函数和CompareThe Letters函数同时组合和打印。当它提示用户猜测时,它还会打印出CompareTheLetters函数的第一个if语句。我做错了什么?
#define _CRT_SECURE_NO_WARNINGS
#define MAXGUESSES 4
#include <stdio.h>
void GuessItRules(); //Game instructions
int PlayOneGame(char letter); //Runs a game
char GetTheLetter(); //Prompts user to guess and returns guess
int IsAWinner(char letter, char userguess); //Returns either a 1 or a 0 depending on if the user guessed correctly
void CompareTheLetters(char letter, char userguess); //Prints message dependent on whether guess comes before, after, or is the letter
int main()
{
//declare additional variables
//declare FILE pointer
FILE* PTR;
int numOfgames; //number of games user wants to play
int i; //iterator
char letter; //letter from file
int win; //variable for the return of the PlayOneGame function
//display instructions
GuessItRules();
//connect to the file HINT: use fopen
PTR = fopen("letters.txt", "r");
//get number of games to play
printf("Let's start playing!\n\nHow many games would you like to play (1-5)?\n\n");
scanf("%d", &numOfgames);
//this for loop will allow the player to play more than one game
//without recompiling
for (i = 0; i < numOfgames; i++)
{
//get a letter from file - use fscanf
fscanf(PTR, "%c", &letter);
//Play one game (Call PlayOneGame function) - remember the function has an int return type
win = PlayOneGame(letter);
//tell the player if they have won or lost (test the variable win)
if (win == 1)
printf("Congrats! You guessed the correct letter!!\n\n");
else if (win == 0)
printf("I'm sorry you did not guess the correct answer :( The letter was %c\n\n", letter);
}
//close file
fclose(PTR);
return 0;
}
//Function definitions
void GuessItRules()
{
printf("Welcome to the Guess the Letter Game!\n-------------------------------------\n\n");
printf("You will have 4 chances to guess the letter per a game\n\n");
}
int PlayOneGame(char letter)
{
int numOfguesses = 0;
int winOrLose = 0; //should be intialized
char userguess; // user guess
//As long as the user has not used up the maximum number
//of guesses and has not guessed correctly
//the game will continue using this while loop
while (numOfguesses < MAXGUESSES && winOrLose == 0)
{
//function call to GetTheletter - returns to userguess variable
userguess = GetTheLetter();
//function call to IsAWinner - returns to winOrLose variable (0 or 1)
winOrLose = IsAWinner(letter, userguess);
//function call to CompareTheLetters - gives the user a message
CompareTheLetters(letter, userguess);
//update counter for number of guesses used
numOfguesses = numOfguesses + 1;
}
return winOrLose; //(0 or 1)
}
char GetTheLetter()
{
char userguess;
printf("Please enter a guess \n\n");
scanf("%c", &userguess);
return (userguess);
}
int IsAWinner(char letter, char userguess)
{
if (userguess == letter)
return (1);
else
return (0);
}
void CompareTheLetters(char letter, char userguess)
{
if (userguess < letter)
{
printf("The letter you are trying to guess comes after %c\n\n", userguess);
}
else if (userguess > letter)
{
printf("The letter you are trying to guess comes before %c\n\n", userguess);
}
else if (userguess == letter)
{
printf("The letter is %c!!!!\n\n", userguess);
printf("Congrats!!! You did it!!\n\n");
}
}
答案 0 :(得分:1)
问题出在于你要求用户写一封信来猜测。
当您使用%c
格式说明符scanf
时,换行符满足此格式说明符。由于您之前输入的输入缓冲区中会留下换行符,因此会选择该换行符。
在格式字符串的开头添加一个空格以吸收换行符:
scanf(" %c", &userguess);
请注意,其他格式说明符(例如%d
和%f
)会跳过换行符,与%c
不同。因此,%c
仅需要跳过换行符的主要空间。