-1

I am making a Hangman game and I have encountered an issue. In this code the program selects a random word from a finite number of values, then it takes the random word and masks it so that in future coding it will allow the user to guess the letters.

The issue lies in the masking, I have no idea how to link it to future code for the loop and counter as well as output the masked word, as can be shown with;

        mask[i] = '_';
        printf("%s", mask[i]);

I get the error, Param_(2) in call to 'printf' must be the address of a string. Actual type 'int' which when I run I get a weird error where I have to halt debugger.

Any solutions?

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include <time.h>
#include <string>
#define ARRAY_SIZE 10

int main()
{
    //randomwordgenerator
    char word[ARRAY_SIZE][200] = { "tiger", "lion", "elephant", "zebra", "horse", "camel", "deer", "crocodile", "rabbit", "cat" };


    int x = 0;
    srand(time(0));

    x = rand() % ARRAY_SIZE;

    system("pause");//will pause the rand function

    //masking and unmasking word
    char m = strlen(word[x]);//will count the number of letters of the random word
    int mask[200]{};
    for (int i = 0; i < m; ++i) //loop until all leters are masked
    {
        mask[i] = '_';
        printf("%s", mask[i]);
    }
return 0;
}
Ocelotter
  • 1
  • 3
  • 1
    `mask[i]` is a `char` so use `printf("%c", mask[i]);` There's also `putc` and `putchar` to print a `char`. – 001 Aug 18 '22 at 14:21
  • 3
    What's the purpose of the brackets in `int mask[200]{};`? – Fiddling Bits Aug 18 '22 at 14:22
  • Instead of `int mask[200]{};`, zero fill it properly with `int mask[200] = {0};`. I think you then want to copy: `strcpy(mask, word[x]);`. Then later print the `mask` _string_ with `printf("%s", mask);` (no `[i]`). – chux - Reinstate Monica Aug 18 '22 at 14:52
  • More idiomatic C to use `size_t` to store a string's length than `char`. – chux - Reinstate Monica Aug 18 '22 at 14:53
  • Hello, firstly Johnny your solution worked perfectly, thank you. Fiddling are you talking about the {} because I just added them for no reason. Reinstate, I tried Implementing `int mask[200] = {0};` and `strcpy(mask, word[x]);` but I get the error that since mask is an int, I can't use strcpy bcs it's for strings. Chux, oh thanks for the head's up! – Ocelotter Aug 18 '22 at 17:13

1 Answers1

0

You could basically keep your "printf" function call intact if you were to just move it outside of the "for" loop and print the string "mask". Following is a snippet of code taking your initial program above and doing some minor rearranging.

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

#define ARRAY_SIZE 10

int main()
{
    //randomwordgenerator
    char word[ARRAY_SIZE][200] = { "tiger", "lion", "elephant", "zebra", "horse", "camel", "deer", "crocodile", "rabbit", "cat" };
    int x = 0;
    char mask[200];             /* Moved the definition of mask up here*/

    srand(time(0));

    x = rand() % ARRAY_SIZE;

    //system("pause");//will pause the rand function /* Deactivated this for this proof of principle snippet */

    //masking and unmasking word
    char m = strlen(word[x]);   //will count the number of letters of the random word
    strcpy(mask, word[x]);      /* Used the standard strcpy function to copy the word into mask so that it has the proper NULL terminator */

    for (int i = 0; i < m; ++i) //loop until all leters are masked
    {
        mask[i] = '_';          /* Replaces the letters of the copied word with "_" */
    }

    printf("%s\n", mask);       /* Moved this outside of the for loop */
    printf("%s\n", word[x]);    /* Just printing this to prove that the proper number of underline characters were used */

    return 0;
}

The main points to note are:

  • The definition of the mask array was just moved up to the top with the other data definitions.
  • The standard "strcpy" function was used to prepare the mask array with the selected random word. At that point, "mask" contains the selected word.
  • The "for" loop is used as before to replace the letters in the selected word that are stored in the "mask" character array with the underline character. Nothing really changed there.
  • Then, after the "for" loop completes, the mask array is printed via the "printf" function.

As a test, here is a sample of the output from my terminal.

@Una:~/C_Programs/Console/HangEm/bin/Release$ ./HangEm 
______
rabbit

I just temporarily added the word that was being masked to prove that the proper number of underline characters were being printed.

Give that a try and see if that meets the spirit of your project.

NoDakker
  • 3,390
  • 1
  • 10
  • 11