0

I try to program the game Hangman but I can't get it to work. Compiling works but when I execute this program its not displaying the help-array for guessing letters... =(

The game is not finished yet because I can't get this to work.

I tried to hand over the array helpArray of the function helpArrayFunc() to the function drawHelpArray() so it can display the help-array for the letters to guess, but its not doing nothing.

What is the error here?

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

//--- Function Prototypes ---
int * helpArrayFunc(char word[19][20],char,int *,int *,int *,int *);
void drawHelpArray(char word[19][20],int *,int *,int *);
void drawHangMan(int *);
char letterFunc();

int main()
{   
    // Wörter werden initialisiert
    char word[19][20]={"Portal 2","Half-Life 2","Left 4 Dead 2","CS:GO","Rainbow Six","Fallout 4","Bioshock","Call of Duty","DayZ","H1Z1","Outlast","The Stanley Parable","Insurgency","Dying Light","Thief","Assasin's Creed","Wolfenstein","Payday 2","Need For Speed"};  
    // Zufallswort
    int numWord=0;
    srand(time(NULL));
    numWord=rand() % 19;
    // Wortlänge
    int wordLength = strlen(word[numWord]);
    // Fehler
    int mistakes=0;
    // HilfsArray
    int helpArray[wordLength];
    helpArray[wordLength]=0;
    // Buchstabe
    char letter;

    //printf("%s\n\n",word[numWord]); Only for testing

    do
    {
        drawHangMan(&mistakes);
        *helpArray=helpArrayFunc(word,letter,helpArray,&mistakes,&numWord,&wordLength);
        drawHelpArray(word,helpArray,&numWord,&wordLength);
        letter=letterFunc();
        system("cls");
    }
    while(mistakes<11);

    printf("\n\n___________________________\n\n");
    printf(" Copyright\xA9 Thomas Sapelza\n");
    printf("___________________________\n\n");
    system("PAUSE");
    return 0;
}

int * helpArrayFunc(char word[19][20],char letter,int *mistakes,int *helpArray,int *numWord,int *wordLength)
{
    bool mistake=false;

    // Buchstaben überprüfen ob er im Wort enthalten ist
    for(int i=0;i<(*wordLength);i++)
    {
        if(letter==word[*numWord][i])
        {   
            helpArray[i]=1;
            return helpArray;
        }
        else
        {
            helpArray[i]=0;
            mistake=true;
        }
    }

    // Auf fehler überprüfen
    if(mistake) *mistakes++;
}

void drawHelpArray(char word[19][20],int *helpArray,int *numWord,int *wordLength)
{
    // Zeichen die im HilfsArray immer angezeigt werden sollen
    char space='\x20',dash='\x2D',comma='\x27',colon='\x3A';

    // HilfsArray darstellen
    for(int i=0;i<(*wordLength);i++)
    {
        // Wenn der Wert von helpArray auf 1 ist wird der Buchstabe angezeigt
        if(helpArray[i]==1)
        {
            printf("%c ",word[*numWord][i]);
        }

        // Wenn der Wert von helpArray auf 0 ist wird ein Zeichen dargestellt
        if(helpArray[i]==0)
        {
            // Zeichen die immer enthalten sein sollen bzw. 
            if(word[*numWord][i]==space)
            {
                printf("  ");
            }
            else if(word[*numWord][i]==dash)
            {
                printf("- ");
            }
            else if(word[*numWord][i]==comma)
            {
                printf("' ");
            }
            else if(word[*numWord][i]==colon)
            {
                printf(": ");
            }
            else
            {
                printf("_ ");
            }
        }
    }
    printf("\n\n");
}

char letterFunc() // Buchstabe einlesen
{
    char letter;
    printf("\nGeben Sie ein Buchstabe ein: ");
    fflush(stdin);
    scanf("%c",&letter);
    return letter;
}

void drawHangMan(int *mistakes)
{

switch(*mistakes) 
    {
    case 0:
        printf("\n\nFalsche Buchstaben: %d\n\n",*mistakes);
        printf("\n");
        printf("\n");
        printf("\n");
        printf("\n");
        printf("\n");
        printf("\n");
        printf("____________\n\n");
    break;
    case 1:
        printf("\n\nFalsche Buchstaben: %d\n\n",*mistakes);
        printf("\n");
        printf("  |\n");
        printf("  |\n");
        printf("  |\n");
        printf("  |\n");
        printf("  |\n");
        printf("__|_________\n\n");
    break;
    case 2:
        printf("\n\nFalsche Buchstaben: %d\n\n",*mistakes);
        printf("  _______\n");
        printf("  |\n");
        printf("  |\n");
        printf("  |\n");
        printf("  |\n");
        printf("  |\n");
        printf("__|_________\n\n");
    break;
    case 3:
        printf("\n\nFalsche Buchstaben: %d\n\n",*mistakes);
        printf("  _______\n");
        printf("  |/\n");
        printf("  |\n");
        printf("  |\n");
        printf("  |\n");
        printf("  |\n");
        printf("__|_________\n\n");
    break;
    case 4:
        printf("\n\nFalsche Buchstaben: %d\n\n",*mistakes);
        printf("  _______\n");
        printf("  |/   | \n");
        printf("  |    O \n");
        printf("  |\n");
        printf("  |\n");
        printf("  |\n");
        printf("__|_________\n\n");
    break;
    case 5:
        printf("\n\nFalsche Buchstaben: %d\n\n",*mistakes);
        printf("  _______\n");
        printf("  |/   | \n");
        printf("  |    O \n");
        printf("  |    |\n");
        printf("  |    |\n");
        printf("  |\n");
        printf("__|_________\n\n");
    break;
    case 6:
        printf("\n\nFalsche Buchstaben: %d\n\n",*mistakes);
        printf("  _______\n");
        printf("  |/   | \n");
        printf("  |    O \n");
        printf("  |   \\|\n");
        printf("  |    | \n");
        printf("  |\n");
        printf("__|_________\n\n");
    break;
    case 7:
        printf("\n\nFalsche Buchstaben: %d\n\n",*mistakes);
        printf("  _______\n");
        printf("  |/   | \n");
        printf("  |    O \n");
        printf("  |   \\|/\n");
        printf("  |    | \n");
        printf("  |\n");
        printf("__|_________\n\n");
    break;
    case 8:
        printf("\n\nFalsche Buchstaben: %d\n\n",*mistakes);
        printf("  _______\n");
        printf("  |/   | \n");
        printf("  |    O \n");
        printf("  |   \\|/\n");
        printf("  |    | \n");
        printf("  |   /\n");
        printf("__|_________\n\n");
    break;
    case 9:
        printf("\n\nFalsche Buchstaben: %d\n\n",*mistakes);
        printf("  _______\n");
        printf("  |/   | \n");
        printf("  |    O \n");
        printf("  |   \\|/\n");
        printf("  |    | \n");
        printf("  |   / \\\n");
        printf("__|_________\n\n");
    break;
    case 10:
        printf("\n\nFalsche Buchstaben: %d\n\n",*mistakes);
        printf("  _______\n");
        printf("  |/   | \n");
        printf("  |    X \n");
        printf("  |   \\|/\n");
        printf("  |    | \n");
        printf("  |   / \\\n");
        printf("__|_________\n\n");
    break;
    }
}
2
  • 1
    I suggest you run your code under a debugger.
    – JJF
    Dec 10, 2015 at 21:35
  • Your compiler must have given you warnings. Never ignore them! Suggest you go back and fix those first. For example, helpArrayFunc is declared to return an int * but the function can exit without returning anything (in the case where it exits the for loop).
    – kaylum
    Dec 10, 2015 at 22:08

1 Answer 1

0

How to assign a function output (array) to another function in c?

A C function cannot output (return) an array. Besides, in helpArrayFunc() the helpArray isn't an array of, but a pointer to int. Though, you don't need the function to return an array, because it operates on the original int helpArray[wordLength] from main() - every change it makes to the array contents via the parameter int *helpArray refers to the original array, so you can drop the wrong assignment *helpArray= (which refers to the first array element) in main() and declare as void helpArrayFunc(…). Consequently, drawHelpArray() receives the original array address with the identical values that helpArrayFunc() left - if they are still wrong, it's helpArrayFunc()'s fault.
Note also that you confused the order of helpArray and mistakes in the call and the definition of helpArrayFunc().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.