Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have my code working properly for one word but when I added in the if else statements at the start and another switch statement i am getting a few errors. Any help would be appreciated :)

here is the code:
C#
#include<stdio.h>
#include<stdlib.h>
#define letters 6
#define letters2 9


main()
{
    srand((unsigned)time(NULL));
    int num = rand() %5 +1 ;

    if(num == 1)
    {
        char word[letters] = {'f','l', 'i', 'c', 'k'};
        char spaces[letters] = {'-', '-', '-', '-', '-'};
    }

    else
    if(num == 2)
    {
        char word2[letters] = {'s','p', 'o', 'r', 't'};
        char spaces[letters] = {'-', '-', '-', '-', '-'};
    }

    else
    if(num == 3)
    {
        char word3[letters] = {'b','o', 'o', 't', 's'};
        char spaces[letters] = {'-', '-', '-', '-', '-'};
    }

    else
    if(num == 4)
    {
        char word4[letters2] = {'f','o', 'o', 't', 'b', 'a', 'l', 'l'};
        char spaces2[letters2] = {'-', '-', '-', '-', '-', '-', '-', '-'};
    }

    else
    if(num == 5 )
    {
        char word4[letters2] = {'s','w', 'i', 'm', 'm', 'i', 'n', 'g'};
        char spaces2[letters2] = {'-', '-', '-', '-', '-', '-', '-', '-'};
    }


    int startmenu, PlayGame, ExitGame;
    int lives = 6;
    int i;
    int j = 0;
    int exit;
    int correct;
    char symb;
    int length = 5;
    printf("Lets PLay Hangman!");
    printf("\n1. Play game\n0. Exit");
    scanf("%d",&startmenu);
    switch(num)
    {
        case 1:
        {
            switch( startmenu )// Menu that gives the user the option to play or not

        {

            case 1:
            {
                while ((lives>0) && (j < length))
                {
                    correct = 0;
                    printf("\nPlease enter a letter: ");
                    do
                    {
                        scanf("%c", &symb);
                        if((symb > 32) && ((symb < 97) || (symb > 122))) // Ascii table used for error cheching of characters that cannot be used
                        {
                            printf("\nInvalid character\nEnter a letter: ");
                        }
                    }while((symb < 97) || (symb > 122));
                        for(i=0;i < length;i++)
                        {
                            if(symb == word[i])
                            {
                                spaces[i] = symb;

                                correct = 1;
                                printf("\nThat was correct! you have %i lives left\n",lives);
                                printf("\nYou have %s\n",spaces);

                                //check if player has won
                                if(word[1]==spaces[1]&&
                                   word[2]==spaces[2]&&
                                   word[3]==spaces[3]&&
                                   word[4]==spaces[4]&&
                                   word[5]==spaces[5])// This is different from my flowchart because I made a mistake in it. The flowchart says you win after guessing one letter correctly
                                {
                                    printf("\nCongtratulations You win :)");
                                    printf("Please press 0 to exit");
                                    scanf("%d",&exit);
                                    switch (exit)
                                    {
                                        return 0;
                                    }
                                }
                            }
                        }
                        if(correct == 0)
                        {
                            lives--;
                            if(lives > 0)
                            {
                                printf("\nSorry that was not right! %i lives left\n",lives);
                            }
                            else
                            {
                                printf("\nSorry You lost! :(\n");
                                printf("Please press 0 to exit");
                                scanf("%d",&exit);
                                switch (exit)
                                {
                                    {
                                        return 0;
                                    }
                                }
                            }
                            printf("\nYou have %s\n",spaces);
                        }
                    }
                }

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

        {
            case 2:
                switch( startmenu )// Menu that gives the user the option to play or not

        {

            case 1:
            {
                while ((lives>0) && (j < length))
                {
                    correct = 0;
                    printf("\nPlease enter a letter: ");
                    do
                    {
                        scanf("%c", &symb);
                        if((symb > 32) && ((symb < 97) || (symb > 122))) // Ascii table used for error cheching of characters that cannot be used
                        {
                            printf("\nInvalid character\nEnter a letter: ");
                        }
                    }while((symb < 97) || (symb > 122));
                        for(i=0;i < length;i++)
                        {
                            if(symb == word2[i])
                            {
                                spaces[i] = symb;

                                correct = 1;
                                printf("\nThat was correct! you have %i lives left\n",lives);
                                printf("\nYou have %s\n",spaces);

                                //check if player has won
                                if(word2[1]==spaces[1]&&
                                   word2[2]==spaces[2]&&
                                   word2[3]==spaces[3]&&
                                   word2[4]==spaces[4]&&
                                   word2[5]==spaces[5])// This is different from my flowchart because I made a mistake in it. The flowchart says you win after guessing one letter correctly
                                {
                                    printf("\nCongtratulations You win :)");
                                    printf("Please press 0 to exit");
                                    scanf("%d",&exit);
                                    switch (exit)
                                    {
                                        return 0;
                                    }
                                }
                            }
                        }
                        if(correct == 0)
                        {
                            lives--;
                            if(lives > 0)
                            {
                                printf("\nSorry that was not right! %i lives left\n",lives);
                            }
                            else
                            {
                                printf("\nSorry You lost! :(\n");
                                printf("Please press 0 to exit");
                                scanf("%d",&exit);
                                switch (exit)
                                {
                                    {
                                        return 0;
                                    }
                                }
                            }
                            printf("\nYou have %s\n",spaces);
                        }
                    }
                }

            case 0:
            {
                return 0;
            }//end case 0
        }
        getchar();
        getchar();
    }
}
Posted
Comments
Sergey Alexandrovich Kryukov 22-Nov-12 11:48am    
Can anyone explain why this is a 3rd question in a raw on hangman? Do you guys get identical assignments at school?!
OK, did you try to look for existing answers? On CodeProject? On the Web? Please do it first...
--SA
schmoo18 22-Nov-12 11:54am    
I searched both and could not find what i was looking for

1 solution

You have variable declarations after some executable code; this is not allowed in pure C. Move the declarations to the beginning of the function block.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 23-Nov-12 1:49am    
:-) Sure, a 5.
--SA

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