子手随机功能帮助 [英] Help with random function for hangman

查看:55
本文介绍了子手随机功能帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码可以正确使用一个单词,但是当我在开头添加if else语句并在另一个switch语句中添加时,出现了一些错误.任何帮助将不胜感激:)

这是代码:

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:

#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();
    }
}

推荐答案

在一些可执行代码之后有变量声明;在纯C语言中是不允许的.将声明移到功能块的开头.
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.


这篇关于子手随机功能帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆