简体   繁体   English

C中的Lettergame提前结束

[英]Lettergame in C ending early

#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#define MAXGUESSES 5
                                                            // function declarations
void GameRules();
int SingleGame(char file_letter);
char RetrieveGuess();
int GuessedIt(char answer, char input_letter);
int main()
{
    int PlayGames = 0,                                      //Variable Declarations
        i = 0;                                              //Variable Declarations
    char correctanswer;                                     //Variable Declarations

    GameRules();                                            //Call of GameRules Function
    FILE *fp;                                               //Opening File
    fp = fopen("lettersin", "r");                           //Opening File for Reading
    printf("How many games would you like to play?1-4:");   //Prompting for Number of Games to Play
    scanf("&d", &PlayGames);                                //Scanning number of games wished to be played, storing it in PlayGames 
    for (i = 0; i<PlayGames; i++)                           //For Loop that lasts until i=playgames
    {
        fscanf(fp, "%c", &correctanswer);                   //function to scan 1 letter from lettersin.txt
        int SingleGame(correctanswer);                      //Play one game
        if (SingleGame == 1)                                //if Single Game returns 1, you win
        {
            printf("You Win!");
        }
        else if (SingleGame == 0)                           //If Single Game returns 0, you lose
        {
            printf("You Lose");
        }
    }
    fclose(fp);                                             //Closes the File
    return 0;
}

void GameRules()                                            //Function that prints the Rules
{
    printf("This is the Letter Game. The goal is to guess the correct letter within 5 attempts. After each guess you will be told whether the letter you attempted to guess comes before or after the letter actually guessed.\n");
}

char RetrieveGuess()                                        //Function to prompt for a guess, then store the guess in an integer that is returned by the function when called
{
    char a;
    printf("Enter a letter");
    scanf("%c", &a);
    return a;
}

int GuessedIt(char answer, char input_letter)               //Function that returns 1 when the answer is correct, or suggests where the correct answer is if the guess is incorrect
{
    if (answer == input_letter)                             //if the guess is the same as the answer, return 1
    { 
        return 1; 
    }
    else if (answer > input_letter)                         //if the guess is incorrect, suggest on how to improve and return 0
    { 
        printf("the correct letter comes before your guess"); return 0; 
    }
    else 
    {
        printf("the correct letter comes after your guess"); return 0;
    }

}


int SingleGame(char file_letter)                            //Function to play 1 game
{
    int numGuesses = 0;                                     //Variable Declarations
    char b;
    while (numGuesses < MAXGUESSES)                         //While Loop that repeats until numguesses=maxguesses
    {
        b=RetrieveGuess();                                  //sets b equal to the value RetrieveGuess returns
        GuessedIt(file_letter, b);                          //uses b and whichever value is entered into SingleGame to determine wheter the answer is correct
        if (GuessedIt == 1)                                 //If function that returns 1 if the guess is correct and ends the function, otherwise it increments numguesses
        {
            return 1;
            numGuesses = 6;
        }
        else numGuesses = numGuesses++;                     //increments numguesses to end loop after 5 guesses

    }
    if (numGuesses == 5)                                    //returns 0 if the letter is not guessed within 5 tries
    {
        return 0;
    }

}

When I try to run my code, I'm getting no error messages, but my code ends after i enter the amount of games i would like to play. 当我尝试运行代码时,没有收到任何错误消息,但是在输入想要玩的游戏数量之后,代码结束了。 There are no errors, other than 'cannot find or open PDB File' and I'm using Microsoft Visual Studio 2013 Express 除了“无法找到或打开PDB文件”之外,没有任何错误,并且我正在使用Microsoft Visual Studio 2013 Express

It's scanf("%d", &PlayGames); scanf("%d", &PlayGames); (change the &d by %d) (将&d更改%d)

在主函数中,您正在使用singlegame-函数名称作为变量...它会引发错误,因为您尚未声明任何此类变量。而是将函数调用singlegame(correctanswer)置于if和else条件中。

Your code should not compile at all. 您的代码根本不应该编译。 I'm surprised you claim it does. 令您惊讶的是,我感到很惊讶。 You are using functions as if they were variables. 您正在使用函数,就好像它们是变量一样。

But in any case, after fixing all those things the code does indeed end prematurely. 但是无论如何,在修复所有这些问题之后,代码确实确实过早地结束了。 The cause is a typo in scanf("&d", &PlayGames); 原因是scanf("&d", &PlayGames);的错字scanf("&d", &PlayGames); . The int format specifier should read %d instead of &d . int格式说明符应读取%d而不是&d

Because of this typo the value of PlayGames is never set and stays at 0. 由于这种错字,因此PlayGames的值永远不会设置为0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM