Hi everyone I had a few more quick questions...I'm trying to right the function that puts the correct letters in the right positons being reported from my previous compareletter function for my game of hang man and its not updating the word in progress array.

Here's my function:

//updates the word in progress array with correct guesses
void updateWIP (char array[], char guess, int result)
{

    if (result !=-1)
    array[result] = guess;

}

Now in my c book , it speaks of a strncpy function but when I tried to use strncpy(array, guess, result); it reported argument type of char is incompatible with parameter type char const. The result is the number of the last correctly reported positon of the correct letter and guess is the current correct letter needing to be copied to the wip array to replace the asterick in that position with whatever guess is.

strncpy() is similar to strcpy(), both functions take two null-terminated character arrays as arguments. So neither of those functions will work in your situation.

Before calling updateWIP() array needs to contain the same number of characters as in the word that the user is trying to guess. So if the word is "hello" then array needs to be initialized with 5 spaces. If that is done your updateWIP() should work, assuming the value of result is one of the numbers 0, 1, 2, 3, and 4.

Yah I got you I did this earlier in my program for the wip array that I'm passing in as array:

    //copy length of wtbg (word to be guessed) to wip (word in         progress) array

    numLetters = strlen(wtbg);

    DisplayArray(wtbg);

    while (numguess != 6)
    {

    //fill wip
    printf("GUESS THIS WORD:\n");
    for (i = 0;i < numLetters; i++)
    {
        wip[i] = '*';

    }

This is my entire hangman program so far if it helps:

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define S 25

//function prototypes

//displays instructions for user
void Instructions();

//displays character arrays 
void DisplayArray(char array[]);

//prompts user to enter a letter 
char GuessLetter();

//compares arrays to see if they match or not
int CompareArray(char arraya[], char arrayb[]);

//function that returns the index of the letter that the user has guessed or 
//-1 if the letter isn't in the word
int CompareLetter(char array[], char guess, int numLetters);

//tells the player if they won the game or not 
void wonGame(int theSame);

//updates the word in progress array with correct guesses
void updateWIP (char array[], char guess, int result);

main()
{
    //declare variables
    FILE *fp;
    int i;
    int x;
    char wtbg[S] = {'\0'};
    char gl[S] = {'\0'};
    char wip[S] = {'\0'};
    char guess = ' ';
    int numguess = 0;
    int numLetters = 0;
    int theSame = 0;
    int wresult = 0;

    //displays instructions for user
    Instructions();

    //get word from file
    fp = fopen ("words.txt", "r");
    fscanf(fp,"%s", wtbg);

    //copy length of wtbg to wip array
    numLetters = strlen(wtbg);

    DisplayArray(wtbg); 

    while (numguess != 6)
    {
    //printf("GUESS=%d", guess);

    //fill  wip
    for (i = 0;i < numLetters; i++)
    {
        wip[i] = '*';
        //printf("%c", wip[i]);
    }
    wip[i] = '\0';

    printf("GUESS THIS WORD:\n");
    DisplayArray(wip);
    guess = tolower(GuessLetter());
    gl[numguess] = (guess);
    printf("\nLetters guessed so far: ");
    DisplayArray(gl);
    wresult = CompareLetter(wtbg, guess, numLetters);
    printf("wresult=%d", wresult);
    updateWIP(wip, guess, wresult);
    //theSame = CompareArray(wtbg, wip, numLetters);
    //printf("\nAre they the same array? %d", theSame);
    //printf("NUM LETTERS = %d", numLetters);

    //update number of guesses
    //wresult = 0;
    numguess++;}

    //printf("GUESS=%d", guess);}

    return 0;

}
//displays instructions for user
void Instructions()
{
    printf("WELCOME TO THE ULTIMATE GAME OF HANGMAN!\n\n");
    printf("PLEASE TAKE A LOOK AT THE FOLLOWING RULES/INSTRUCTIONS!\n\n");
    printf("You will have the opportunity to guess a word.\n\n");
    printf("Guess the letters one at a time.\n\n");
    printf("You can have upto six wrong guesses.\n\n");
    printf("The game ends when either you have guessed:\n");
    printf("--all the letters in a word\n");
    printf("--guessed wrong six times\n\n");
    printf("LET THE GAMES COMMENCE...\n\n");
}

//displays character arrays 
void DisplayArray(char array[])
{
    printf("%s\n", array);

}

//prompts user to enter a letter and stores it in guess array 
char GuessLetter()
{
    char guess;

    printf("\nPlease enter a letter:");
    scanf(" %c", &guess);
    return guess;
}

//compares arrays to see if they match or not
int CompareArray(char arraya[], char arrayb[])
{   
    int theSame;

    theSame = strcmp(arraya,arrayb);

    return theSame;
}

//function that returns the index of the letter that the user has guessed or 
//-1 if the letter isn't in the word
int CompareLetter(char array[], char guess, int numLetters)
{
    int i;
    for (i = 0;i < numLetters; i++)
    {
        //printf("numLetters=%d", numLetters);
        if(array[i] == guess) {
          return i;
        }
    }   //printf("numLetters=%d", numLetters);
        return -1;
}


//tells the player if they won the game or not 
void wonGame(int theSame)
{
    if (theSame == 0)
    {
    printf("\nCONGRATS...YOU WON THE GAME!!!");
    }

    else if (theSame == 1)
    {
    printf("\nSORRY YOU DIDN'T WIN LOOSER..."); 
    }

}

//updates the word in progress array with correct guesses
void updateWIP (char array[], char guess, int result)
{
    printf("GUESS=%c", guess);
    printf("WRESULT=%d", result);
    printf("WIP ARRAY%s\n", array); 

    if (result !=-1)
    array[result] = guess;
}

line 50: This is always reading the first word that's in the text file. You need to read a random word. After you've guessed the word correctly it's not much fun to use the same word every time you run the program.

Actually, now that I think about it you don't need to use a text file at all. Just get a word of random length and fill it with random letters. The word will be meaningless, but will always produce a random set of letters to choose.

lines 62-67: Initialzing wip is being done on every loop iteration, you don't want to do that. Move the while statement on lines 57 and 58 down to below line 67 so that wip is only initialized once before asking for user input.

line 84: You need to stop the loop when the user has entered the correct word, if the word is "one" then you don't want to keep on asking for letters after you've guessed the word. A simple check with strcmp() will solve that little problem.

As far as getting the word that way, thats how it is in the assignment...they provide a file of four words. After the user finishes playing the game, I have to read in the second word in and so on.

I can't believe that I missed 62-67...that's why it wasn't updating. I guess you should really take a break and get away from your code once in a while. Thanks! :)

I just updated the program so that at the end of the while loop

theSame = CompareArray(wtbg, wip);

gets called and uses strcmp() to compare both the wip & wtbg arrays.

One more question...if I wanted to prompt the player if they would like to play again, I know I would have to reinitialize most of my games variables but how exactly would that work with using

    //get word from file
    fp = fopen ("words.txt", "r");
    fscanf(fp,"%s", wtbg);

Would I just use the same code again? How would the file pointer know to skip kind and goto the next word in the file? Does it leave the file pointer just after the first word after I first used it ?

Thanks!

The easiest way is to rearrange main() just a little by putting almost everything in another loop. Notice that the file is only opened once at the beginning of main() so that each scanf() will read the next word in the file. You need to add a check for EOF.

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define S 25


main()
{
    //declare variables
    FILE *fp;
    fp = fopen ("words.txt", "r");
    for(;;)
    {
        int i;
        int x;
        char wtbg[S] = {'\0'};
        char gl[S] = {'\0'};
        char wip[S] = {'\0'};
        char guess = ' ';
        int numguess = 0;
        int numLetters = 0;
        int theSame = 0;
        int wresult = 0;

        //displays instructions for user
        Instructions();

        //get word from file
        fscanf(fp,"%s", wtbg);

        //copy length of wtbg to wip array
        numLetters = strlen(wtbg);

        DisplayArray(wtbg); 

        //printf("GUESS=%d", guess);

        //fill  wip
        for (i = 0;i < numLetters; i++)
        {
            wip[i] = '*';
            //printf("%c", wip[i]);
        }
        wip[i] = '\0';

        while (numguess != 6)
        {
            printf("GUESS THIS WORD:\n");
            DisplayArray(wip);
            guess = tolower(GuessLetter());
            gl[numguess] = (guess);
            printf("\nLetters guessed so far: ");
            DisplayArray(gl);
            wresult = CompareLetter(wtbg, guess, numLetters);
            printf("wresult=%d", wresult);
            updateWIP(wip, guess, wresult);
            //theSame = CompareArray(wtbg, wip, numLetters);
            //printf("\nAre they the same array? %d", theSame);
            //printf("NUM LETTERS = %d", numLetters);

            //update number of guesses
            //wresult = 0;
            numguess++;
            // ask if you want to guess another word goes here.
        }

        //printf("GUESS=%d", guess);}
    }
    return 0;

}

ok thanks so much !

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.