gpt4 book ai didi

c - 如何在C中的printf语句中打印循环结果

转载 作者:行者123 更新时间:2023-11-30 16:40:21 25 4
gpt4 key购买 nike

我构建了一个字母猜谜游戏,基本上用户输入他们想要玩的游戏数量,我从文件中收到一封字母,用户输入一个猜测,然后我告诉他们他们是否正确。我想要做的是在输入游戏数量后打印“Ready for game #1”,并在获得猜测字母之前打印“Getting Guess #1”。我从循环“i”知道它,但我似乎无法弄清楚。这是我的代码

C

#define _CRT_SECURE_NO_WARNGINGS
#include<stdio.h>
#include<ctype.h>
#define MAXGUESSES 5

//this function provides instructions to the user on how to play the game
void GameRules();
//this function runs one game.
//input: character from the file, void return type
//all other functions to Play one round of a game
//are called from within the GuessTheLetter function
void GuessTheLetter(char);
//this function prompts the player to make a guess and returns that guess
//this function is called from inside the GuessTheLetter( ) function described above
char GetTheGuess();
//this function takes two arguments, the guess from the player
//and the solution letter from the file.
//The function returns 1 if the guess matches the solution and returns a 0 if they do not match
//This function also lets the user know if the guess comes alphabetically before or after the answer
int CompareLetters(char, char);


int main() {
FILE *inPtr;
int numGames, i = 0;
char letter;//letter from file

printf("Welcome to the Letter Guessing Game\n");
GameRules();

printf("How many games? (1 to 8)\n");
scanf("%d", &numGames);
inPtr = fopen("letterList.txt", "r");
for (i = 0; i < numGames; i++) {

fscanf(inPtr, " %c", &letter);
letter = tolower(letter);
GuessTheLetter(letter);
}
return 0;

}

void GuessTheLetter(letter) {

int win = 0;
int numGuesses = 0;
char guess;
while (numGuesses < MAXGUESSES && win == 0) {

guess = GetTheGuess();
guess = tolower(guess);
win = CompareLetters(letter, guess);
numGuesses++;
}

if (win == 1) {

printf("And you Won !!!\n");

}
else {

printf("SORRY, you did not win this round\n");
}

}

char GetTheGuess() {
char guessEntered;
printf("Enter a guess\n");
scanf(" %c", &guessEntered);
return guessEntered;
}

int CompareLetters(letter, guess) {

int winGet;
if (letter == guess) {
printf("The solution and the guess are the same ( %c )", letter);
winGet = 1;
}
else if (letter != guess) {
printf("The solution comes before your guess ( %c )", letter);
winGet = 0;
}
else {
printf("Error!");
}

return winGet;
}
void GameRules() {

printf("First, you will enter the number of games you want to play(1 - 8 games)\n");
printf("For each game you will have 5 chances to guess each letter\n");
printf("Let's begin:\n\n");

}

最佳答案

现在还好吗?

#define _CRT_SECURE_NO_WARNGINGS
#include<stdio.h>
#include<ctype.h>
#define MAXGUESSES 5

//this function provides instructions to the user on how to play the game
void GameRules();
//this function runs one game.
//input: character from the file, void return type
//all other functions to Play one round of a game
//are called from within the GuessTheLetter function
void GuessTheLetter(char);
//this function prompts the player to make a guess and returns that guess
//this function is called from inside the GuessTheLetter( ) function described above
char GetTheGuess();
//this function takes two arguments, the guess from the player
//and the solution letter from the file.
//The function returns 1 if the guess matches the solution and returns a 0 if they do not match
//This function also lets the user know if the guess comes alphabetically before or after the answer
int CompareLetters(char, char);


int main() {
FILE *inPtr;
int numGames, i = 0;
char letter;//letter from file

printf("Welcome to the Letter Guessing Game\n");
GameRules();

printf("How many games? (1 to 8)\n");
scanf("%d", &numGames);
inPtr = fopen("letterList.txt", "r");
for (i = 0; i < numGames; i++) {
printf("Ready to play Game %d\n", i);
fscanf(inPtr, " %c", &letter);
letter = tolower(letter);
GuessTheLetter(letter);
}
return 0;

}

void GuessTheLetter(char letter) {

int win = 0;
int numGuesses = 0;
char guess;
while (numGuesses < MAXGUESSES && win == 0) {

guess = GetTheGuess();
guess = tolower(guess);
win = CompareLetters(letter, guess);
numGuesses++;
}

if (win == 1) {

printf("And you Won !!!\n");

}
else {

printf("SORRY, you did not win this round\n");
}

}

char GetTheGuess() {
char guessEntered;
printf("Enter a guess\n");
scanf(" %c", &guessEntered);
return guessEntered;
}

int CompareLetters(char letter, char guess) {

int winGet;
if (letter == guess) {
printf("The solution and the guess are the same ( %c )\n", letter);
winGet = 1;
}
else if (letter != guess) {
printf("The solution comes before your guess ( %c )\n", letter);
winGet = 0;
}
else {
printf("Error!\n");
}

return winGet;
}
void GameRules() {

printf("First, you will enter the number of games you want to play(1 - 8 games)\n");
printf("For each game you will have 5 chances to guess each letter\n");
printf("Let's begin:\n\n");

}

关于c - 如何在C中的printf语句中打印循环结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46701073/

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