Click here to Skip to main content
15,894,740 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi am having trouble with the arrays of letters and hyphens in my hangman project
for example:


C#
#include<stdio.h>
#include<stdlib.h>
#define letters 80

main()
{
    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");
    scanf("%d",&startmenu);

    switch( startmenu )// Menu that gives the user the option to play or not

    {

        case 1:
        {

            for(i=0; i < length ; i++)
            {
                word[length]=0;
            }
            word[length] = 0;
            printf("%c", spaces);
            while ((lives>0) && (j < length))
            {
                correct = 0;
                printf("Please enter a letter: ");
                do
                {
                    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
                        break;
                    }
                }
                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)
                    {
                        lives--;
                        if(lives > 0)
                        {
                            printf("Sorry that was not right! %i lives left.\n",lives);
                        }
                        else
                        {
                            printf("Sorry You lost! :(\n");
                        }
                        printf("You have %s\n",word);
                    }
                }
            }




        }
        case 0:
        {
            return 0;
        }//end case 0
    }
    getchar();
    getchar();
}



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
Updated 21-Nov-12 8:39am
v4
Comments
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?

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900