0

My program runs fine currently, except for the part where it should be displaying the gradually hanging man.

#include <stdio.h>
#include <string.h>

#define SIZE 30

void game(char w[], char g[], int len);
void hangman(int m);

main() {

char word[SIZE] = "vocal";

int len1 = 0, i;
char guessed[SIZE];

len1 = strlen(word);
guessed[len1];

for(i = 0; i < len1; i++)
    guessed[i] = '*';
    guessed[len1] = '\0';
printf("%s\n", guessed);

game(word, guessed, len1);
}

void game(char w[], char g[], int len) {
char buffer[100];
char letter = '\0';
int j;
int misses = 0, hits = 0;

while(g != w && misses != 8) 
{
    printf("Enter a guess: ");
    fgets(buffer, 100, stdin);
    sscanf(buffer, "%c", &letter);



    for(j = 0; j < len; j++) 
    {
        if(letter == w[j]) 
        {

            g[j] = letter;
            hits++;
            printf("%s\n", g);
            if(hits == len) {
            printf("Congrats, you won!\n");
            misses = 8;

        }

I think the logic for this else is what's wrong, but I don't know how to fix it.

        else 
        {
            if( j == len && letter != w[j]) 
            {
                misses++;
                hangman(misses);
            }
        }
        }
    }

}
}

void hangman(int m) {

switch(m) {
        case 1:
            {
                printf("     ----------\n");
                printf("    |/         |\n");
                printf("    |         (_)\n");
                printf("    |\n");
                printf("    |\n");
                printf("    |\n");
                printf("    |\n");
                printf("    |\n");
                printf("____|____\n");
                break;
            }
            case 2:
            {
                printf("     ----------\n");
                printf("    |/         |\n");
                printf("    |         (_)\n");
                printf("    |          |\n");
                printf("    |\n");
                printf("    |\n");
                printf("    |\n");
                printf("    |\n");
                printf("____|____\n");
                break;
            }
            case 3:
            {
                printf("     ----------\n");
                printf("    |/         |\n");
                printf("    |         (_)\n");
                printf("    |         \\|\n");
                printf("    |\n");
                printf("    |\n");
                printf("    |\n");
                printf("    |\n");
                printf("____|____\n");
                break;
            }
            case 4:
            {
                printf("     ----------\n");
                printf("    |/         |\n");
                printf("    |         (_)\n");
                printf("    |         \\|/\n");
                printf("    |\n");
                printf("    |\n");
                printf("    |\n");
                printf("    |\n");
                printf("____|____\n");
                break;
            }
            case 5:
            {
                printf("     ----------\n");
                printf("    |/         |\n");
                printf("    |         (_)\n");
                printf("    |         \\|/\n");
                printf("    |          |\n");
                printf("    |\n");
                printf("    |\n");
                printf("    |\n");
                printf("____|____\n");
                break;
            }
            case 6:
            {
                printf("     ----------\n");
                printf("    |/         |\n");
                printf("    |         (_)\n");
                printf("    |         \\|/\n");
                printf("    |          |\n");
                printf("    |         /\n");
                printf("    |\n");
                printf("    |\n");
                printf("____|____\n");
                break;
            }
            case 7:
            {
                printf("     ----------\n");
                printf("    |/         |\n");
                printf("    |         (_)\n");
                printf("    |         \\|/\n");
                printf("    |          |\n");
                printf("    |         / \\ \n");
                printf("    |\n");
                printf("    |\n");
                printf("____|____\n");
                break;
            }
            case 8:
                {
                printf("     ----------\n");
                printf("    |/         |\n");
                printf("    |         (_)\n");
                printf("    |         \\|/\n");
                printf("    |          |\n");
                printf("    |         / \\ \n");
                printf("    |\n");
                printf("    |\n");
                printf("____|____\n");
                printf("You lost.");
                break;
                }
            }
}

If anyone can help me, I'd greatly appreciate it!

cluless1
  • 25
  • 2
  • well, the indentation does indicate that it probably is wrong, but you'll need to include more of the code from the very first part of the conditionals, ie the first `if` to the last closing brace – Madivad Nov 12 '15 at 22:24
  • I wouldn't say that it "runs fine". All it does is keep prompting me for more guesses. – 200_success Nov 12 '15 at 22:34
  • @Madivad Those are all the if statements.. This is all of the code – cluless1 Nov 12 '15 at 22:59
  • What do you think `guessed[len1];` does? What do you think `g != w` does? – David Schwartz Nov 13 '15 at 01:04
  • `guessed[len1];` just makes that array of characters be the same size as the word that the user is supposed to guess. `g!=w` as a condition to that while loop, was making it run again as long as guessed != word to be guessed. After further testing, I finally got rid of it though. – cluless1 Nov 13 '15 at 01:23

1 Answers1

0

Ok, there were some logical errors has you expected. I have tried to do the minimal set of changes. Now it works but you can surely improve it. For example the game should tell the user if he/she wants to play again.

(include also stdlib.h for the function exit())

void game(char w[], char g[], int len)
{
    char buffer[100];
    char letter = '\0';
    int j;
    int misses = 0, hits = 0;
    int global_miss = 0;

    while(g != w && misses != 8)
    {
        printf("Enter a guess: ");
        fgets(buffer, 100, stdin);
        sscanf(buffer, "%c", &letter);
        misses = 0;

        for(j = 0; j < len; j++)
        {
            if(letter == w[j])
            {
                misses = 0;
                hits++;
                g[j] = letter;
                printf("%s\n", g);
                if(hits == len) {printf("Congrats, you won!\n"); exit(111);}
            } else {
                misses++;
                fprintf(stdout, "misses = %d\n", misses);
                if (misses == len) {
                    fprintf(stdout, "This letter is wrong! Try again");
                    global_miss += 1;
                    hangman(global_miss);
                    if (global_miss == 8) {
                        printf("Sorry you lose!\n"); 
                        exit(111);
                    }
                }
            }
        }
    }
}

You should also remove this unused expression in your main:

guessed[len1];

And, please, define int main (see also: What should main() return in C and C++? if you are interested in the debate int vs void main)

Community
  • 1
  • 1
terence hill
  • 3,144
  • 11
  • 28
  • That link was very helpful and I will no longer be using `main()`. Also, thanks so much for the code. I fixed it up some more and was able to get it working exactly as the guidelines say – cluless1 Nov 13 '15 at 01:19