The game is to play hangman, if guessed right or wrong it will ask the user if they want to play another game (w/ a different word).

My code repeats the game with the user enters anything (even q) but doesnt quit when 'q' is entered. I've been trying to do a while(q != 0) at the beginning but I keep doing it wrong and I don't know how to get the next word in the file to be used in the next game.

Please help me figure out what I'm doing wrong, this is my code:

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

/* contant declarations */
#define MAX 6

/* function prototypes */
void clrscrn( void );
void draw_hangman( int incorrect_tries );
void DisplayAstrisks(char word[], char visible_word[]);
void Instructions();
int QuitorNot(char quit);
void DidyouWin(char visible_word[], char word[], char incorrect_letters[], char guess);
//void Compare(char visible_word[MAX], char incorrect_letters[MAX]);
//int FindLtr(char word[], char guess, char visible_word[], int repeat_flag, int correct_guesses, int correct_flag, char incorrect_letters[], int tries);


int main()
{
    /* variable declarations */
    char len       = 0; /* length of word char array */
    int count             = 0, /* for word char array       */
    tries             = 0, /* total tries user has used */
    //num_vis_chars     = 0, /* # of visible characters   */
    correct_guesses   = 0, /* # of correct guesses      */
    correct_flag      = 0, /* was guess correct?        */
    repeat_flag       = 0; /* was guess a repeat?       */
    FILE* infile = NULL;
    char guess;
    int quit=0;

    /* array declarations */
    char word[255]              = " ";
    char incorrect_letters[255] = " ";
    char visible_word[len]; /* displays correct guesses */

    /* getting word from file */


      infile = fopen("words.txt", "r");


    fscanf(infile, " %s", word);

    len = strlen( word );

    DisplayAstrisks(word, visible_word);



    Instructions();

    draw_hangman( tries );



    while( tries < MAX )
    {

        if(quit == 0)
        {


        printf( "WORD: %s\n", visible_word );
        printf( "Incorrect Guesses: %s\n", incorrect_letters );
        printf( "\nGuess a letter: " );
        scanf( " %c", &guess );

        //Compare(visible_word, incorrect_letters);

        /*match guess against previous guesses*/
        for( count = 0; count < len; count++ )
            if( guess == visible_word[count] || guess == incorrect_letters[count] )
            {
                repeat_flag  = 1;
                correct_flag = 1;
                break;
            }



        if( repeat_flag == 0 )

            //DidyouWin(visible_word, word, incorrect_letters, guess);
        /* check for matches in string*/
            for( count = 0; count < len; count++ )
            {
                if( guess == word[count] )
                {
                    visible_word[count] = guess;
                    correct_guesses++;

                    if( correct_guesses == len )
                    {
                        printf( "      --------------YOU WIN!--------------" );
                        printf( "\n                  ANSWER: %s\n", visible_word );


                        QuitorNot(0);

                    }

                    correct_flag = 1;
                }
            }

        if( correct_flag == 0 )
        {
            incorrect_letters[tries] = guess;
            tries++;


        }
        repeat_flag  = 0;
        correct_flag = 0;


        /* reset flags */


        clrscrn();

        draw_hangman( tries );
    }

    printf( "      --------------YOU LOSE!--------------\n" );
    printf( "                  THE ANSWER: %s\n", word );
    QuitorNot(1);





}




}






/**************************************************************/
/* clrscrn() clears the screen */
void clrscrn( void )
{
    int x = 0;

    for( x = 0; x < 3; x++ )
        printf( "\n" );
}


/**************************************************************/
/* draw_hangman() displays the hangman */
void draw_hangman( int incorrect_tries )
{
    /*
     ___
     |  &
     |  O
     | /|\
     | / \
     |
     ---
     */

    switch( incorrect_tries )
    {
        default:
        {
            puts( "\n  ___" );
            puts( " |  &" );
            puts( " |" );
            puts( " |" );
            puts( " |" );
            puts( " |" );
            puts( "---" );
            break;
        }


        case 1:
        {
            puts( "\n  ___" );
            puts( " |  &" );
            puts( " |  O" );
            puts( " |" );
            puts( " |" );
            puts( " |" );
            puts( "---" );
            break;
        }

        case 2:
        {
            puts( "\n  ___" );
            puts( " |  &" );
            puts( " |  O" );
            puts( " | /" );
            puts( " |" );
            puts( " |" );
            puts( "---" );
            break;
        }

        case 3:
        {
            puts( "\n  ___" );
            puts( " |  &" );
            puts( " |  O" );
            puts( " | /|" );
            puts( " |" );
            puts( " |" );
            puts( "---" );
            break;
        }

        case 4:
        {
            puts( "\n  ___" );
            puts( " |  &" );
            puts( " |  O" );
            puts( " | /|\\" );
            puts( " |" );
            puts( " |" );
            puts( "---" );
            break;
        }

        case 5:
        {
            puts( "\n  ___" );
            puts( " |  &" );
            puts( " |  O" );
            puts( " | /|\\" );
            puts( " | /" );
            puts( " |" );
            puts( "---" );
            break;
        }

        case 6:
        {
            puts( "\n  ___" );
            puts( " |  &" );
            puts( " |  O" );
            puts( " | /|\\" );
            puts( " | / \\" );
            puts( " |" );
            puts( "---" );
            break;
        }

    }
}


/**************************************************************/
void DisplayAstrisks(char word[], char visible_word[])
{
    int count=0;
    char len=strlen(word);

    /* initialize visble_word */
    for( count = 0; count < len; count++ )
        visible_word[count] = '*';

    visible_word[len] = '\0';
}


/**************************************************************/
void Instructions()
{
    printf("--------------------WELCOME TO--------------------\n");
    printf("H   H                GGGGGG                        \n");
    printf("H   H    AA    N   N G      MM   MM    AA    N   N \n");
    printf("HHHHH   A  A   NN  N G  GGG M M M M   A  A   NN  N \n");
    printf("H   H  AAAAAA  N N N G    G M  M  M  AAAAAA  N N N \n");
    printf("H   H A      A N  NN GGGGGG M     M A      A N  NN \n\n\n");


    printf("      -The computer will pick a secret word.\n");
    printf("      -The game is to guess this word with \n");
    printf("       guessing one letter at a time.\n");
    printf("      -Guess wrong six times and game over.\n\n\n");
    printf("      --------------START--------------\n\n\n");
    return;

}


/**************************************************************/
int FindLtr(char word[], char guess, char visible_word[], int repeat_flag, int correct_guesses, int correct_flag, char incorrect_letters[], int tries)
{

    int count;
    char len = strlen(visible_word);
    if(repeat_flag == 0 )
    /* check for matches in string */
        for( count = 0; count < len; count++ )
        {
            if( guess == word[count] )
            {
                visible_word[count] = guess;
                correct_guesses++;

                if( correct_guesses == len )
                {
                    puts( "\n\n      --------------YOU WIN!--------------" );
                    printf( "WORD: %s\n", visible_word );
                    return 1;
                    //exit( 0 );
                }

                correct_flag = 1;
            }
        }

    if( correct_flag == 0 )
    {
        incorrect_letters[tries] = guess;
        tries++;
        return -1;
    }
    return 0;
}





int QuitorNot(char quit)
{

    printf("                  Try again?\n   Enter Q to QUIT, anything else to play\n");
    scanf(" %c", &quit);

    while((quit == 'q') || (quit == 'Q'))
    {
        return 1;
    }
    if((quit != 'q') && (quit != 'Q'))
    {
        return 0;


}
    return 0;
}






void DidyouWin(char visible_word[], char word[], char incorrect_letters[], char guess)
{
    int repeat_flag = 0;
    int correct_flag = 0;
    int count;
    int correct_guesses = 0;
    int tries = 0;

    char len = strlen(visible_word);

    if( repeat_flag == 0 )
        for(count = 0; count < len; count++)
        {
            if( guess == word[count] )
            {
                visible_word[count] = guess;
                correct_guesses++;

                if( correct_guesses == len )
                {
                    printf( "      --------------YOU WIN!--------------" );
                    printf( "\n                  ANSWER: %s\n", visible_word );
                    QuitorNot(0);

                }

                correct_flag = 1;
            }
        }

    if( correct_flag == 0 )
    {
        incorrect_letters[tries] = guess;
        tries++;


    }
    repeat_flag  = 0;
    correct_flag = 0;

}

Recommended Answers

All 6 Replies

There are somany mistakes in the code.

QuitorNot(0);

arguement is passed( which is infact not needed i guess) , but not handled.

in side function QuitorNot(0);

while((quit == 'q') || (quit == 'Q'))
{
return 1;
}

not required. just if will do.

and the return value of QuitorNot() is not collected.

there is a space before %c in scanf

scanf(" %c", &quit);

My code repeats the game with the user enters anything (even q) but doesnt quit when 'q' is entered. I've been trying to do a while(q != 0) at the beginning but I keep doing it wrong and I don't know how to get the next word in the file to be used in the next game.

The 'quit' is not getting updated any where.

i think thats the cause for repeating the game when user enters anything

while( tries < MAX )
{
if(quit == 0)
{

Thank you, I was able to fix it with an if statement and now it runs how it should only that it runs with the same word. I need it to get the next string from the file.

I'm new with strings so I'm confused on how the next word is not being scaned when the function returns it to the main function.

This in my code so far:

#include <stdio.h>
#include <string.h>
#include <ctype.h>
/* contant declarations */
#define MAX 6


/* function prototypes */
void clrscrn( void );
void draw_hangman( int incorrect_tries );
void DisplayAstrisks(char word[], char visible_word[]);
void Instructions();
char quitornot(char *again);


int main()
{
    /* variable declarations */
    char len              = 0; /* length of word char array */
    int count            = 0, /* for word char array       */
    tries                = 0, /* total tries user has used */
    correct_answer       = 0, /* # of correct guesses      */
    correct_guess        = 0, /* was guess correct?        */
    repeat_guess          = 0; /* was guess a repeat?       */
    int again             = 0;
    FILE* infile;
    char guess;


    /* array declarations */
    char word[255]              = " ";
    char incorrect_letters[255] = " ";
    char correct_letters[225]   = " ";
    char visible_word[len]; /* displays correct guesses */




    Instructions();


    infile = fopen("words.txt", "r");



    fscanf(infile, " %s", word );
    len = strlen( word );



    DisplayAstrisks(word, visible_word);

    draw_hangman( tries );




    while( tries < MAX )
    {




        printf( "WORD: %s\n", visible_word );
        //printf(" secret word: %s\n", word);
        printf( "                                       Your Guessed letter\n");
        printf("                                Incorrect: %s\n", incorrect_letters );
        printf( "                                Correct: %s\n", correct_letters );


        printf( "\nGuess a letter: " );
        scanf( " %c", &guess );
        guess = tolower(guess);



        /*match guess against previous guesses*/
        for( count = 0; count < len; count++ )
            if( guess == visible_word[count] || guess == incorrect_letters[count] )
            {
                repeat_guess  = 1;
                correct_guess = 1;
                break;
            }



        if( repeat_guess == 0 )

            //DidyouWin(visible_word, word, incorrect_letters, guess);
        /* check for matches in string*/
            for( count = 0; count < len; count++ )
            {

                if( guess == word[count]  )
                {
                    correct_letters[correct_answer]=guess;

                    visible_word[count] = guess;

                    correct_answer++;

                    correct_guess = 1;

                    if( correct_answer == len )
                    {
                        printf( "      --------------YOU WIN!--------------" );
                        printf( "\n                  ANSWER: %s\n", visible_word );

                        quitornot(&again);

                    }

                }
            }

        if( correct_guess == 0 )
        {

            incorrect_letters[tries] = guess;

            tries++;

        }

        repeat_guess  = 0;
        correct_guess = 0;


        clrscrn();

        draw_hangman( tries );


    }

    printf( "      --------------YOU LOSE!--------------\n" );
    printf( "                  THE ANSWER: %s\n", word );

    fclose(infile);

    quitornot(&again);


}



/**************************************************************/
/* clrscrn() clears the screen */
void clrscrn( void )
{
    int x = 0;

    for( x = 0; x < 3; x++ )
        printf( "\n" );
}


/**************************************************************/
/* draw_hangman() displays the hangman */
void draw_hangman( int incorrect_tries )
{
    /*
     ___
     |  &
     |  O
     | /|\
     | / \
     |
     ---
     */

    switch( incorrect_tries )
    {
        default:
        {
            printf( "\n  ___\n" );
            printf( " |  &\n" );
            printf( " |\n" );
            printf( " |\n" );
            printf( " |\n" );
            printf( " |\n" );
            printf( "---\n" );
            break;
        }


        case 1:
        {
            printf( "\n  ___\n" );
            printf( " |  &\n" );
            printf( " |  O\n" );
            printf( " |\n" );
            printf( " |\n" );
            printf( " |\n" );
            printf( "---\n" );
            printf("You have 5 more incorrect guesses left\n");
            break;
        }

        case 2:
        {
            printf( "\n  ___\n" );
            printf( " |  &\n" );
            printf( " |  O\n" );
            printf( " | /\n" );
            printf( " |\n" );
            printf( " |\n" );
            printf( "---\n" );
            printf("You have 4 more incorrect guesses left\n");
            break;
        }

        case 3:
        {
            printf( "\n  ___\n" );
            printf( " |  &\n" );
            printf( " |  O\n" );
            printf( " | /|\n" );
            printf( " |\n" );
            printf( " |\n" );
            printf( "---\n" );
            printf("You have 3 more incorrect guesses left\n");
            break;
        }

        case 4:
        {
            printf( "\n  ___\n" );
            printf( " |  &\n" );
            printf( " |  O\n" );
            printf( " | /|\\\n" );
            printf( " |\n" );
            printf( " |\n" );
            printf( "---\n" );
            printf("You have 2 more incorrect guesses left\n");
            break;
        }

        case 5:
        {
            printf( "\n  ___\n" );
            printf( " |  &\n" );
            printf( " |  O\n" );
            printf( " | /|\\\n" );
            printf( " | /\n" );
            printf( " |\n" );
            printf( "---\n" );
            printf("You have 1 incorrect guesses left\n");
            break;
        }

        case 6:
        {
            printf( "\n  ___\n" );
            printf( " |  &\n" );
            printf( " |  O\n" );
            printf( " | /|\\\n" );
            printf( " | / \\\n" );
            printf( " |\n" );
            printf( "---\n" );
            printf("You have no more guesses left\n");
            break;
        }

    }
}


/**************************************************************/
void DisplayAstrisks(char word[], char visible_word[])
{
    int count=0;
    char len=strlen(word);

    /* initialize visble_word */
    for( count = 0; count < len; count++ )
        visible_word[count] = '*';

    visible_word[len] = '\0';
}


/**************************************************************/
void Instructions()
{
    printf("--------------------WELCOME TO--------------------\n");
    printf("H   H                GGGGGG                        \n");
    printf("H   H    AA    N   N G      MM   MM    AA    N   N \n");
    printf("HHHHH   A  A   NN  N G  GGG M M M M   A  A   NN  N \n");
    printf("H   H  AAAAAA  N N N G    G M  M  M  AAAAAA  N N N \n");
    printf("H   H A      A N  NN GGGGGG M     M A      A N  NN \n\n\n");


    printf("      -The computer will pick a secret word.\n");
    printf("      -The game is to guess this word with \n");
    printf("       guessing one letter at a time.\n");
    printf("      -Guess wrong six times and game over.\n\n\n");
    printf("      --------------START--------------\n\n\n");
    return;

}

/**************************************************************/

char quitornot(char *again)
{

    char *response = again; //response local varibale for again
    *response ='\0'; //set to character 0 in order to run through


    printf("                  Try again?\n   Enter Q to QUIT, anything else to play\n");
    scanf(" %s", response);
    *response = tolower(*response);


    while(*response != 'q')

    {
        return main();

    }
    if (*response == 'q')
    {

        exit(0);

    }
    return 0;
}
/**************************************************************/

Problem 1:

All the scanf's you used have space before format specifier(%c , %s ), instead of using scanf for reading characters use getchar() functions, and for strings use fgets.

scanf( " %c", &guess );
scanf(" %s", response);

scanf does not work well with multiline strings.

Problem 2:

char len = 0; /* length of word char array */
char visible_word[len];

here

sizeof(visible_word)

is zero , and you are trying to store some values in visible_word in function DisplayAstrisks
allocate some memory for visible_word

char visible_word[255];

Add following to your code , above the main function

static int no_of_chars_read =0;

and after opening file

infile = fopen("words.txt", "r");
fseek(infile,no_of_chars_read, SEEK_SET);
fscanf(infile, " %s", word );
len = strlen( word );
no_of_chars_read = len;

That will solve your problem.

small correction

no_of_chars_read = no_of_chars_read + len+1;

Thank you for your help, i wish i wouldve seen this earlier, my teacher has never actually taught us those methods which seem a lot simplier.

I was able to find a different aproach that worked for me before i got to read this.

I added a while(quit !='q') loop before scanning the file but after opening it.

then change the location of my quitornot function call to the end of the code and changed my quitornot function to:

void quitornot(char *again)
{

    char *response = again; //response local varibale for again
    *response ='1'; //set to character 0 in order to run through


    while(*response != 'q')
    {

        printf("                    Try again?\n   Enter Q to QUIT, anything else to play\n");
        scanf(" %s", response);
        *response = tolower(*response); //sets the response to lower case

        return ;

    }

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.