in

there is something wrong about char arrays I guess


I have to make a program like hangman. But in my code I don’t have to draw it , all my program have to do is selecting a word randomly and print it like _____ . Then the user will enter a letter and program have to check the letter if it’s a letter of the selected word. After that if it is, it have to print the word like ___a___a . Here is an example.

Selected Word: engineering

“___________” (11 letters)

Enter a letter: e

“e____ee____”

Here is my code. It’s a little bit different ,but I don’t think that’s the problem that makes me asking the question.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
int main()
{
    char words[10][20] = {"computer","engineer","ubuntu","desktop","download","terminal","programming","shell","system","success"};
    char taken;
    int random = rand()%10;
    int tries = 2 * strlen(words[random]);
    int control,control2;
    char word[20];
    for(int i = 0; i< strlen(word); i++)
    {
        word[i] = '_';
    }
    printf("Welcome to the game!nIn this game, you have %d tries and that means the word you'll find has %d letters.If your tries will be finished, don't worry! Program will add 5 more tries.nGood Luck!n",tries,tries/2);
    puts(word);
    printf("nEnter a letter: ");
    taken = getc(stdin);

    while(tries != 0)
    {
    control = 0;
    for(int i = 0; i < 30; i++)
    {
        if(taken == words[random][i])
        {
            word[i] = taken;
            control++;
        }
        if(words[random][i] == '')
        break;
    }

    if(control == 0)
    {
    printf("nWord doesn't include this letter.n");
    }

    puts(word);
    printf("n");

    control2 = 0;
    for(int i = 0; i < strlen(word);i++)
    {
        if(word[i] != '_')
        control2++;
    }
    if(control2 == 0)
    {
        printf("You find the word! Congratulations!!n");
        break;
    }

    if(tries == 0)
    {
        printf("+5 tries!n");
        tries = 5;
    }

    }
    return 0;

}

The output is:

If you find the problem, please answer my question.



Source: https://stackoverflow.com/questions/70557300/there-is-something-wrong-about-char-arrays-i-guess

a small Golang library providing hardware inspection and discovery for Linux and Windows

How to get TP of closed position?