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

标签 c

我构建了一个字母猜谜游戏,基本上用户输入他们想要玩的游戏数量,我从文件中收到一封字母,用户输入一个猜测,然后我告诉他们他们是否正确。我想要做的是在输入游戏数量后打印“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/

相关文章:

c - 从cmd通过mingw编译报错,codeblocks可以编译执行

c++ - 我如何理解我在 openMP 中的代码?

c - 普通代码的意外编译错误

c++ - 连接 UDP 套接字,但仍从其他源接收数据报

c - 初始化 C 结构体与分配成员

c - 如何在不使用 system、popen、fork、exec 的情况下在 C/Linux 中执行外部命令?

c - 限制发送队列中udp数据包的数量

c - GTK 应用程序不会从对话框中退出

c++ - "munch"是什么?它是如何与 cfront 一起使用的

C数独回溯求解器预检函数