C语言中的字母猜测游戏

C语言中的字母猜测游戏,c,C,我得到的错误是游戏号码没有正确显示。 例如,我选择玩两个游戏。一、 然后,试着猜对第一个数字。显示了解决方案,然后跳到下一个游戏。然而,第二场比赛显示为第三场而不是第二场。 我又试了一次。这次,我猜错了字母1次,猜对了1次。在第二次猜测之后,游戏显示了解决方案,然后停止了游戏,尽管我选择玩2个游戏,但只玩了1个游戏。 信函列表文件中的字母顺序为 D B G W Q T R Y U X 所以第一个游戏以“d”开始,然后是“B”,然后等等。。。。 错误显示为程序本身去掉了偶数。 我不知道是怎么回事

我得到的错误是游戏号码没有正确显示。 例如,我选择玩两个游戏。一、 然后,试着猜对第一个数字。显示了解决方案,然后跳到下一个游戏。然而,第二场比赛显示为第三场而不是第二场。 我又试了一次。这次,我猜错了字母1次,猜对了1次。在第二次猜测之后,游戏显示了解决方案,然后停止了游戏,尽管我选择玩2个游戏,但只玩了1个游戏。 信函列表文件中的字母顺序为 D B G W Q T R Y U X 所以第一个游戏以“d”开始,然后是“B”,然后等等。。。。 错误显示为程序本身去掉了偶数。 我不知道是怎么回事

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


void LetterGuessRules();
void GuessTheLetter(char);
char GetTheGuess();
int CompareLetters(char, char);

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

    //display the game rule
    LetterGuessRules();

    printf("\nHow many games do you want to play? (Max 10) >> ");
    scanf("%d", &numGames);
    printf("\n\n************************************************\n");
    inPtr = fopen("letterList.txt", "r");

    for (i = 0; i < numGames; i++)
    {
        //get a solution letter from file - use fscanf
        fscanf(inPtr," %c", &letter);
        //change the solution to lowercase
        letter = tolower(letter);
        //print the solution back onto the screen to test

        //Close this when play the game to hide the foreseen solution
        printf("\nThe letter is %c\n", letter);   

        //Number of match
        printf("\t\tGame %d\n", i += 1);

        //call the GuessTheLetter function and pass it the solution
        GuessTheLetter(letter);
    }
    fclose(inPtr);
    return 0;
}

void GuessTheLetter(char letter)
{
    int win = 0;
    int numGuesses = 0;
    char myGuess;

    while (numGuesses < MAXGUESSES && win == 0)
    {
        //get a guess from the user  by calling the GetTheGuess function
        myGuess = GetTheGuess();

        //change the guess to lowercase
        myGuess = tolower(myGuess);

        //win = call the function to compare the guess with the solution
        win = CompareLetters(letter, myGuess);

        numGuesses++;//count the number of guesses so far

        //use conditions to let the user know if they won or lost the round of the game
        if (win == 0)
        {
            printf("\nOops its wrong.\n");
            if (myGuess < letter)
            {
                printf("Your guessed letter -%c- comes before the solution\n", myGuess);
                printf("Please guess again :)\n");
            }
            else if (myGuess > letter)
            {
                printf("Your guessed letter -%c- comes after the solution\n", myGuess);
                printf("Please guess again :)\n");
            }
            if (numGuesses == MAXGUESSES && win == 0)
                printf("Aw you have lost this game!");
                printf("\n");
        }
        else if (win == 1)
        {
            printf("\nYou have guessed it right!\n");
            printf("Wonderful! You ACE'd this match!\n");
            printf("\n");
            printf("**** If you play more than 1 game, new match will automatically start ****\n");
            printf("\tYou only need to keep guessing for the next letter\n");
            printf("------------------------------------------------------------------------------");
            printf("\n");
        }
    }
}

char GetTheGuess()
{
    char myGuess;
    printf("\t_______________________");
    printf("\n\t|What's your guess? >> "); 
    scanf(" %c", &myGuess);

    return myGuess;
}

void LetterGuessRules()
{
    printf("\n*** Instruction: ");
    printf("\nYou will have 5 attempts to guess the right answer");
    printf("\nIf you guess it right, the game will end with your victory.");
    printf("\nOtherwise, you will have to guess again.");
    printf("\nPlease have fun!");
}

int CompareLetters(char letter, char myGuess)
{
    if (letter == myGuess)
    {   
        return 1;
    }
    else
    {
        return 0;
    }
}
\define\u CRT\u SECURE\u NO\u警告
#包括
#包括
#定义最大猜测5
无效规则();
无效猜测字母(字符);
char GetTheGuess();
int CompareLetters(char,char);
int main()
{
文件*inPtr;
int numGames,i=0;
字符字母;
//显示游戏规则
规则();
printf(“\n您想玩多少游戏?(最多10个)>>”;
scanf(“%d”和numGames);
printf(“\n\n***********************************************************\n”);
inPtr=fopen(“letterList.txt”,“r”);
对于(i=0;i字母)
{
printf(“您猜到的字母-%c-位于解决方案\n”myGuess)之后);
printf(“请再猜一次:)\n”);
}
如果(numguesss==MAXGUESSES&&win==0)
printf(“哇,你输了这场比赛!”);
printf(“\n”);
}
否则如果(win==1)
{
printf(“\n您猜对了!\n”);
printf(“太棒了!你在这场比赛中得了A!\n”);
printf(“\n”);
printf(“****如果您玩了一个以上的游戏,新的比赛将自动开始****\n”);
printf(“\t您只需要不断猜测下一个字母\n”);
printf(“--------------------------------------------------------------------------------------------------------------”);
printf(“\n”);
}
}
}
char GetTheGuess()
{
查我猜;
printf(“\t\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu”);
printf(“\n\t |你猜怎么着?>>”;
scanf(“%c”和&myGuess);
返回myGuess;
}
无效规则()
{
printf(“\n***指令:”);
printf(“\n您将有5次尝试猜测正确答案”);
printf(“\n如果你猜对了,游戏将以你的胜利而结束。”);
printf(“\n否则,您必须再次猜测。”);
printf(“\n请玩得开心!”;
}
int CompareLetters(字符字母,字符myGuess)
{
如果(字母==myGuess)
{   
返回1;
}
其他的
{
返回0;
}
}

问题是您将
i
增加了两次。首先在
for
循环中,然后在此处:

    //Number of match
    printf("\t\tGame %d\n", i += 1);
这可能是因为没有它,你得到了
游戏0
。简单的解决方法是从1开始循环,而不是从0开始

因为
i
的意思不仅仅是“循环迭代器”,所以我把它叫做更具描述性的东西,比如
gameNum

/* from 1 to numGames */
int gameNum;
for( gameNum = 1; gameNum <= numGames; gameNum++ ) {
    ...
}
/*从1到numGames*/
内配子数;

对于(gameNum=1;gameNum这是学习如何使用调试器的绝佳机会。使用调试器,您可以逐行查看代码,以查看发生了什么以及程序正在执行什么。您还可以监视变量及其值,并查看它们在程序过程中的变化。了解如何使用调试器是一项重要任务我不适合任何程序员,即使是业余程序员。这是一个很好的建议。我真的没有想到调试器。谢谢!谢谢你的帮助!我现在一定会尝试修复它。:)
#include <errno.h>   /* for errno */
#include <string.h>  /* for strerror() */
#include <stdlib.h>  /* for exit() */

inPtr = fopen("letterList.txt", "r");
if( inPtr == NULL ) {
    fprintf(stderr, "Could not open letterList.txt: %s\n", strerror(errno));
    exit(1);
}