0

I'm not complaining anything about the code results but, when I run the code, the IDE immediately stops working.

I tried many IDEs and editors and still doesn't do anything and there is no error or warning.

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

#define NUM_STRING 4
#define MAX_STRING_SIZE 40

char* getWord()
{
    static char words[NUM_STRING][MAX_STRING_SIZE] = {
        "game",
        "hello",
        "program",
        "pointer"
    };
    int randomIndex = rand() % 4;
    char* selectedWord = words[randomIndex];
    return selectedWord;
}

char GuessLetter()
{
    char letter;
    int value=1;

    while(value)
    {
        printf("\nmake a guess: ");
        if(scanf("%c", &letter) != 1)
        {
            printf("Please re-enter a letter");
        }
        else
        {
            value = 0;
        }
    }
return letter;
}

int checkchar(char* PW)
{
    int i, len = strlen(PW);
    for(i=0; i < len; i++)
    {
        if(PW[i]=='_')
        {
            return 1;
        }
    }
return 0;
}
char* printafter( char *currentWord, int len, char letter, char *PW)
{
    int i;

    for(i=0; i<len; i++)
    {
        if(letter == currentWord[i])
        {
            PW[i] = letter;
        }
        printf("%c ", PW[i]);
    }
return PW;
}

void startGame()
{
    char* currentWord = getWord();
    char *PartialWord=NULL;
    int i, length = strlen(currentWord);

    printf("%s\n", currentWord);
    for(i=0; i<length; i++)
    {
        PartialWord[i] = '_';
        printf("_ ");
    }

    if(!(PartialWord = (char*)malloc(length)))
       {
           printf("Sorry there was an allocation error! ");
           exit(1);
       }

    while(checkchar(PartialWord))
    {
        PartialWord = printafter(currentWord, length, GuessLetter(), PartialWord);
    }
}
int main() {
    srand(time(NULL));
    startGame();
    return 0;
}
Dave Andersen
  • 5,337
  • 3
  • 30
  • 29

1 Answers1

2

The main problem in your code is that you are trying to assign data to the PartialWord array before you have allocated it! This causes undefined behaviour and almost anything can happen - including a partial output of the data, followed by a program crash.

Just move the malloc code to before the for loop in the startGame function (as shown below) ... and remember to free() the memory when you've finished with it:

void startGame()
{
    char* currentWord = getWord();
    char* PartialWord = NULL;
    int i, length = strlen(currentWord);

    // You MUST have this allocation BEFORE you assign the '_' characters in the for loop!
    if (!(PartialWord = malloc(length))) { // Also, yoi don't need to (and shouldn't) cast the malloc return
        printf("Sorry there was an allocation error! ");
        exit(1);
    }

    printf("%s\n", currentWord);
    for (i = 0; i < length; i++) {
        PartialWord[i] = '_'; // In your code, PartialWord is NULL when you do this!
        printf("_ ");
    }

    while (checkchar(PartialWord)) {
        PartialWord = printafter(currentWord, length, GuessLetter(), PartialWord);
    }

    free(PartialWord); // Remember to free the memory when you're done with it!
}

On the issue of whether or not to cast the result of malloc, see here: Do I cast the result of malloc?.

Feel free to ask for any further clarification and/or explanation.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83