Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C
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 Smile | :)
 
here is the code:
#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 22-Nov-12 5:20am
Comments
Sergey Alexandrovich Kryukov at 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 at 22-Nov-12 11:54am
   
I searched both and could not find what i was looking for

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
Sergey Alexandrovich Kryukov at 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)

  Print Answers RSS
0 Dnyaneshwar@Pune 1,300
1 Sergey Alexandrovich Kryukov 660
2 OriginalGriff 309
3 ravikhoda 300
4 Manas Bhardwaj 240
0 Sergey Alexandrovich Kryukov 11,194
1 OriginalGriff 6,867
2 Peter Leow 4,795
3 Abhinav S 3,838
4 Maciej Los 3,575


Advertise | Privacy | Mobile
Web02 | 2.8.140421.2 | Last Updated 22 Nov 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid