Click here to Skip to main content
13,045,540 members (70,604 online)
Rate this:
Please Sign up or sign in to vote.
See more:
Hi am having trouble with the arrays of letters and hyphens in my hangman project
for example:

#define letters 80
    char word[letters] = {'t','r', 'i', 'c', 'k'};
    char spaces[letters] = {'-', '-', '-', '-', '-'};
    char used[USED] = {' ', ' ',' ',' ',' ',' ',' '};
    int startmenu, PlayGame, ExitGame;
    int lives = 6;
    int i;
    int j = 0;
    int correct;
    char symb;
    int length = 5;
    printf("Lets PLay Hangman!");
    printf("\n1. Play game\n0. Exit");
    switch( startmenu )// Menu that gives the user the option to play or not
        case 1:
            for(i=0; i < length ; i++)
            word[length] = 0;
            printf("%c", spaces);
            while ((lives>0) && (j < length))
                correct = 0;
                printf("Please enter a letter: ");
                    scanf("%c", &symb);
                    if((symb > 32) && ((symb < 97) || (symb > 122))) // Ascii table used for error cheching
                        printf("Invalid character\nEnter a letter: ");
                }while((symb < 97) || (symb > 122));
                for(i=0; i<length; i++)
                    if(symb == word[i])
                        printf("This character has been used already\n");//I did not include used characters in my flowchart but im adding it in as an extra error check
                if(i == length)
                    for(i=0;i < length;i++)
                        if(symb == word[i])
                            word[i] = symb;
                            correct = 1;
                            printf("That was correct:D you have %i lives left.\n");
                            printf("You have %s\n",spaces);
                    if(correct == 0)
                        if(lives > 0)
                            printf("Sorry that was not right! %i lives left.\n",lives);
                            printf("Sorry You lost! :(\n");
                        printf("You have %s\n",word);


        case 0:
            return 0;
        }//end case 0

That is my code so far. I know there are a good few issues in there that need to be sorted but the main problem I was having was to get my guess to replace the hyphen.
Posted 21-Nov-12 7:33am
Updated 21-Nov-12 8:39am
JackDingler 21-Nov-12 13:39pm
Please show your relevant code.
schmoo18 21-Nov-12 13:55pm
for(i=0; i < length ; i++)
spaces[i]= word[i];
word[length] = 0;
printf("%c", spaces);
while ((lives>0) && (j < length))

it is the second line of code that i am having the problem with what to put in
JackDingler 21-Nov-12 14:06pm
You can use 'Improve Question' to update your original question and add formatted code there.

I have some suggestions.
1. Make your buffers bigger. I don't know what the value for 'length' is, but I suspect it's writing past the end of the buffer that you allocated.
2. Create a third buffer for the results. Don't change the values of your static data.
3. Though you are trying to null terminate your output, you're assigning the terminating character to the wrong array, 'word[length] = 0;'
schmoo18 21-Nov-12 14:16pm
I have the question updated now. Thanks for the help it is much appreciated:)
JackDingler 21-Nov-12 14:19pm
You're still defining 'letters' as 5, and 'length' as 6, meaning you're writing to a mystery memory location when you do this,'word[length] = 0;'

Make 'letters' bigger. Perhaps 80 is a good value?
schmoo18 21-Nov-12 14:23pm
ok I have done that but it is now only printing out the word trick and not showing the hyphens?
JackDingler 21-Nov-12 14:33pm
See this snippet?

if(symb == word[i])
word[i] = symb;

Do see what's wrong with it?
schmoo18 21-Nov-12 14:35pm
Is it just putting the same value back into word[i]?
JackDingler 21-Nov-12 14:39pm
Yes, and 'spaces' remains unchanged.
schmoo18 21-Nov-12 14:41pm
so should I replace word[i] with spaces[i]?
JackDingler 21-Nov-12 14:43pm
Give it a try. :)
schmoo18 21-Nov-12 14:49pm
when I compiled it is still not showing the hyphens and also every time I guess a letter correctly it just tells me that this character has already been used?
JackDingler 21-Nov-12 15:16pm
Right. In your code, you check to see if the they guessed the right letter. If they got a letter correct, you tell them that this letter has already been used.

You need a separate buffer for used letters.

Walk through this with the debugger, and see what is going on with your program flow. the debugger is your best friend in figuring out any logical flaws in your code.

I am about to become unavailable. I may or may not respond to further questions during the T-Day Holiday.
schmoo18 21-Nov-12 17:28pm
Thank you very much for your help ill do that and hopefully I can get it working. Happy holidays :D
JackDingler 21-Nov-12 14:44pm
Experiment with the code. You'll learn from it.
JackDingler 21-Nov-12 14:34pm
Further, you're incrementing 'i' inside the loop, when the for statement also has an increment.

Take out the inner increment.
JackDingler 21-Nov-12 14:21pm
Also, create a separate array for used letters. Try not to reuse variables for multiple purposes. It makes the code muddied and hard to understand.
schmoo18 21-Nov-12 14:27pm
Ill add that in now
nv3 21-Nov-12 17:38pm
So much "intensive care" deserves a little thank you. I voted one of you other solutions as I can't do anything here. Thanks, Jack.
Sri_Karthikeya 1-Feb-13 0:45am
You have to define the USED in link section.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web01 | 2.8.170713.1 | Last Updated 21 Nov 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100