Hello everyone,

I'm trying to do a game of hang man by comparing whats in one char array with another with a for loop with the compareword function. I tested it out with the wrong letters and had it return -1 so that parts working. What isn't fully working is when I guess the right letter. You have six chances and when I put in the correct letters I get -1 for the first 2 letters, 2 for the 3rd (which is right) and 2 for the last which is wrong.

Also, I'm trying to add * to the wip array but when I pass it to the displayarray function, it doesn't display unless I use a %c instead of %s. My instructor told me to put a %s outside of the for loop, but its still not displaying.

Any ideas ?

Thanks.

#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 and stores it in guess array 
void GuessLetter(char array[], int guess);

//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 CompareWord(char arraya[], char arrayb[], int size);

main()
{
    //declare variables
    FILE *fp;
    int i;
    int x;
    char wtbg[S] = {'\0'};
    char gl[S] = {'\0'};
    char wip[S] = {'\0'};
    int guess = 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 (guess != 6)
    {
    //printf("GUESS=%d", guess);

    //display wip
    printf("GUESS THIS WORD:\n");
    for (i = 1;i <=numLetters; i++)
    {
        wip[i] = '*';
        //printf("%c", wip[i]);
    }
      //wip[i] = '\0';

    GuessLetter(gl, guess);
    printf("\nLetters guessed so far: ");
    DisplayArray(gl);
    DisplayArray(wip);
    wresult = CompareWord(wtbg, gl, numLetters);
    printf("%d", wresult);
    //theSame = CompareArray(wtbg, wip, numLetters);
    //printf("\nAre they the same array? %d", theSame);

    //if (theSame == 0)
    //{
    //printf("\nCONGRATS...YOU WON THE GAME!!!");
    //}

    //else if (theSame == 1)
    //{
    //printf("\nSORRY YOU DIDN'T WIN LOOSER...");   
    //}
    //printf("NUM LETTERS = %d", numLetters);

    //update number of guesses
    guess++;}

    //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 
void GuessLetter(char array[], int guess)
{

    printf("\nPlease enter a letter:");
    scanf(" %c", &array[guess]);
    array[guess] = tolower (array[guess]);
}

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

    theSame = strcmp(arraya,arrayb);

    return theSame;
}

int CompareWord(char arraya[], char arrayb[], int size)
{
    int i;

    for (i = 1;i < size, i++;)
    if(arraya[i] == arrayb[i])
    {
        return i;}

    else 
        return -1;

}

Recommended Answers

All 7 Replies

CompareWord() returns on the first character. If the first character of each array are the same then CompareWord() returns i, which will always be 1, otherwise it always returns -1.

Is there only one word in each of the two arrays? e.g. not a sentence? If so, then you need to set a flag to indicate the two words are the same so that the function will compare all the characters in the word

int CompareWord(char arraya[], char arrayb[], int size)
{
   int flag = 1; // assume arrraya == arrayb
   int i;
   for(i = 0; i < size; i++)
   {
      if( arraya[i] != arrayb[i])
      {
         flag = 0; // not the same
         break; // exit the loop
       }
    }
    return (flag == 1) ? 1 : -1;
}

Or, an alternative way to do it is to just call strcmp(). Assumes both arrays are null-terminated strings and only contain one word.

  int CompareWord(char arraya[], char arrayb[], int size)
    {
       return (strcmp(arraya,arrayb) == 0) ? 1 : -1;
    }

Yes wtbg (word to be guessed) is a single word read in from a file to the array and gs (guessed letters) is an array of letters entered in by the user stored in the gl array.

Yes I have a function that compares the entire word to another using strcmp, but it's not so useful if a user doesn't guess each word exactly in order. That's for the final check, but I need CompareWord to work so that when they guess letters out of order, it can return the position of the correct letter to main so I can fill that letter into wip (words in progress) by replacing one of the astericks with that letter in the correct possiton.

Thanks, I'll try adding that flag variable to the CompareWord function.

I tried adding the flags into the function and its just giving -1 for everything now, including if the letter is in the word.

Oh, now I understand. The arrayb is just a string of letters in any order. In that case you can use a function such as strchr() to see if a letter in arrayb is contained in arraya.

int CompareWord(char arraya[], char arrayb[], int size)
{
    int flag = 1; // assume all letters in arrayb are in arraya
    int i;
    for(i = 0; i < size; i++)
    {
       if( strchr(arraya,arrayb[i]) == NULL)
       {
           flag = -1;
           break;
       }
    }
    // now return the result
    return flag;
}

it can return the position of the correct letter to main so I can fill that letter into wip (words in progress) by replacing one of the astericks with that letter in

Then why are you passing the entire arrayb instead of a single letter? How is the function supposed to know which letter to compare? And if that's what you want it to do then use a different function name that better describes it's purpose.

Doesn't it know from the index? I've been doing printf statements of both array a & b inside the function and its printing the ascii value of k position 0 for my first word that's read in, kind in the wtbg (word to be guessed) array with the correct postion 0 in my gl (guessed letters) array and returning -1 when it isn't. However, the value for i is the same as the ascii code for k or -1 for the rest of the for loop and I don't know why. Maybe because of it missing the flag you mentioned? Here's my modified code.

int CompareLetter(char arraya[], char arrayb[], int size)
{
    int i;
    for (i = 0;i < size; i++)
    {
        if(arraya[i] == arrayb[i]) {
          return i;
        }
    }
        return -1;
}

And yes, I've since updated my function name. I'm also going to try the code you just posted and see if works and get back to you.

Thanks!

Doesn't it know from the index?

what index?? I think the third parameter to the function is the number of characters in arrayb. not the index in arrayb that needs to be checked. Lets say the user enters the letter 'a', the letter 'a' is then appended to the other letters in arrayb. How's CompareLetter() supposed to know which letter in arrayb the user just entered?

Why not just pass the letter to CompareLetter() instead of the entire array of characters entered? For example:

int CompareLetter(char arraya[], char LetterToCheck)
{
    if( strchr(array1,LetterToCheck) != NULL)
       return 1;
    return -1;
}

Yes your right comparing just the individual letter first and then adding it to the guessed letters array later in main has proven to work. Here's my code that is working perfectly the way I wanted it to.

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;
}

Ignore the printf's , they are just tests. Thanks again for the suggestion, I've got two more days to get the rest of this program working I'm looking forward to the challenge. :)

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.