0

I built a letter guessing game and basically the user enters the number of games they want to play, I get a letter from the file, user enters a guess, and i tell them if they are right or not. What i want to do is print "Ready for game #1" after they enter the number of games and also "Getting guess #1" before getting the guess letter. I know its from the loop "i" but i can't seem to figure it out. Here is my code

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");

}
Jagr
  • 484
  • 2
  • 11
  • 31
  • 2
    This looks strikingly similar to [C Program, getting error about uninitialized variable, the program never ends](https://stackoverflow.com/questions/46679238/c-program-getting-error-about-uninitialized-variable-the-program-never-ends) Pay particular attention to how `printf (" ==> Game %d <==\n\n", i + 1);` is handled in the selected answer. – David C. Rankin Oct 12 '17 at 03:51
  • `int CompareLetters(letter, guess) {` -> What happened to the types for the arguments? – Ed Heal Oct 12 '17 at 03:54
  • Also read the manual pages for `scanf` and `fopen` - Take note of the return values - they are useful – Ed Heal Oct 12 '17 at 03:55
  • @DavidC.Rankin thanks for that link, his question helped and i saw how he did it his way by setting it i=1 and instead of me doing it 1 – Jagr Oct 12 '17 at 03:59
  • Sure, just note, that everything in C is indexed from `zero`. So while there is nothing wrong with `for (i = 1; i <=numGames; i++)`, know the normal iteration loops are `for (i = 0; i < numGames; i++)`, that's why I used `i + 1` for the line number `:)` – David C. Rankin Oct 12 '17 at 04:35
  • Ohhh i see thank you :D – Jagr Oct 14 '17 at 03:29

1 Answers1

0

Is it ok now?

#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");

}
Alex
  • 94
  • 9
  • `printf("Ready to play Game %d", i);` is in the correct place. (you may want to add `\n\n` after `%d`) I would suggest compiling with *warnings enabled* (e.g. `/Wall` (or at least `/W3`) and verify there are no other warnings. You will want to look at how ALL input is validated in the other answer. – David C. Rankin Oct 12 '17 at 03:57
  • Thanks for the help but @DavidC.Rankin got it – Jagr Oct 12 '17 at 04:03