Novaea
Novaea

Reputation: 35

Certain statements printing twice

In a hangman program I'm writing, words with two of the same letter, such as "eel" or "bee," upon the user entering "e" the first time and then trying to enter "e" for the second prompt, will display "You've already guessed this letter" twice. How can I fix this?

#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>


int main(){

char w[13][3] = {
        { 'c', 'a', 't' }, //0
        { 'd', 'o', 'g' }, //1
        { 'r', 'a', 't' }, //2
        { 'e', 'e', 'l' }, //3
        { 'c', 'o', 'w' }, //4
        { 'o', 'w', 'l' }, //5
        { 'e', 'm', 'u' }, //6
        { 'b', 'a', 't' }, //7
        { 'e', 'l', 'k' }, //8
        { 'p', 'i', 'g' }, //9
        { 'b', 'e', 'e' }, //10
        { 'h', 'e', 'n' }, //11
        { 'f', 'o', 'x' }, //12
};

char u,
    newline,
    dis[16];

int random,
    guesses = 3,
    finish = 0;

_Bool successfulGuess = false;

srand(time(NULL));
random = rand() % 13;

    printf("Animal %d\n", random); //check random number
    printf("---------\n\n");
    printf("Enter a letter: ");
    u = getchar();
    newline = getchar();

    for (int i = 0; i < 3; i++){

        if (w[random][i] == u){
            successfulGuess = true;
            dis[i] = u;
        }
        else {

            dis[i] = '_';
        }
    }

    for (int j = 0; j < 3; j++){
        dis[j] = dis[j];

    }

    printf("\n");

    for (int i = 0; i < 3; i++){
        printf("%c", dis[i]);
    }

    if (successfulGuess == false){
        --guesses;
    }
    printf("\n\nGuesses left: %d", guesses);
    printf("\n\n");

while (guesses > 0){
    finish = 0;
    successfulGuess = false;
    printf("Enter a letter: ");
    u = getchar();
    newline = getchar();

    for (int i = 0; i < 3; i++){

        if (u == dis[i]){
            successfulGuess = true;
            printf("\nYou already guessed this letter.\n");
            printf("\ninput = dis[i]\nGuesses left: %d\n\n", guesses);
        }
        else if (w[random][i] == u){
            successfulGuess = true;
            dis[i] = u;
            printf("\ninput = array char\nGuesses left: %d\n\n", guesses);
        }
    }
        for (int i = 0; i < 3; i++){
            printf("%c", dis[i]);
        }

        if (successfulGuess == false){
            guesses--;
            printf("\n\nbool statement\nGuesses left: %d\n\n", guesses);
        }

        if (guesses == 0){
            printf("Sorry, you've run out of guesses.");
        }

        for (int i = 0; i < 3; i++) {
            if (dis[i] != '_') {
                finish++;
            }
            if (finish == 3){
                printf("\n\nYou guessed the word!");
                guesses = 0;
            }
            else{
                continue;
            }
        }
        printf("\n\n");
}

system("pause");
}

Upvotes: 2

Views: 71

Answers (1)

JoriO
JoriO

Reputation: 1048

You are looping i three times even if you have already shown the message. I think it should break ather the message:

for (int i = 0; i < 3; i++){

    if (u == dis[i]){
        successfulGuess = true;
        printf("\nYou already guessed this letter.\n");
        printf("\ninput = dis[i]\nGuesses left: %d\n\n", guesses);

        /* BREAK HERE */
        break;
    }

Upvotes: 3

Related Questions