Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C程序在应该执行之前是否返回默认情况?_C_Function_Loops_Switch Statement_Case - Fatal编程技术网

C程序在应该执行之前是否返回默认情况?

C程序在应该执行之前是否返回默认情况?,c,function,loops,switch-statement,case,C,Function,Loops,Switch Statement,Case,我有一个yahtzee项目,我是作为一个项目写的。我被要求定义所有使用的函数和宏。但是,我不知道如何正确地从displayGameMenu函数获取输入,并使用它从switch语句中选择一个案例。它不再显示游戏菜单,而是无限循环默认情况 在到达案例之前,我尝试调用displayGameMenu,它仍然会弹出默认案例。我尝试将int变量设置为等于displayGameMenu,然后将其传递到switch语句中。对不起,我对编码很陌生 代码是: #include <stdio.h> #in

我有一个yahtzee项目,我是作为一个项目写的。我被要求定义所有使用的函数和宏。但是,我不知道如何正确地从displayGameMenu函数获取输入,并使用它从switch语句中选择一个案例。它不再显示游戏菜单,而是无限循环默认情况

在到达案例之前,我尝试调用displayGameMenu,它仍然会弹出默认案例。我尝试将int变量设置为等于displayGameMenu,然后将其传递到switch语句中。对不起,我对编码很陌生

代码是:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

//declare global variables/macros
#define RULES 1
#define GAME 2
#define EXIT 3
#define ROLLS 3

// function prototypes
void gameRules();
void clearScreen();
int displayGameMenu();
void displayRandomDice();
int rollDie();

// main function
int main()
{
    //declare int
    int play = 1;

    //initialize srand with time as seed
    srand(time(0));

    int input = displayGameMenu();

    //initialize while loop controlled by play
    while(play = 1) {



            //list of cases to control function calls
            switch(input)
            {
                case RULES :
                    gameRules();
                    break;
                case GAME :
                    clearScreen();
                    displayRandomDice();
                    break;
                case EXIT :
                    printf("Thank you for playing!");
                    play = 0;
                    break;
                default  :
                    printf("Incorrect option, hit enter and try again");
                    char enter;
                    scanf("%c", &enter);
            }

    }


    // program executed successfully
    return 0;
}

// gameRules function displays the Yahtzee and rules of the game
void gameRules ()
{
    printf ("\t\t\t\tLET'S PLAY YAHTZEE!!! \n\n");
    printf ("RULES OF THE GAME:\n");
    printf ("\t1. The scorecard used for Yahtzee is composed of an upper section and a lower section.\n");
    printf ("\t2. A total of 13 scoring combinations are divided amongst the sections. \n");
    printf ("\t3. The upper section consists of boxes that are scored by summing the value of the dice matching the faces of the box.\n");
    printf ("\t4. If a player rolls four 3's, then the score placed in the 3's box is the sum of the dice which is 12. \n");
    printf ("\t5. Once a player has chosen to score a box, it may not be changed and the combination is no longer in play for future rounds.\n");
    printf ("\t6. If the sum of the scores in the upper section is greater than or equal to 63, then 35 more points are added \n");
    printf ("\tto the players overall score as a bonus. The lower section contains a number of poker like combinations.\n");
}


//clear screen
void clearScreen()
{
    printf("\n\t\t\t\tHit <ENTER> to continue!\n");

    char enter;
    scanf("%c", &enter);

    // send the clear screen command Windows
    system("cls");
    // send the clear screen command for UNIX flavor operating systems
//    system("clear");
}

//display random dice function
void displayRandomDice()
{
        //declare all 6 int type variables
    int numRolls;
    int die1;
    int die2;
    int die3;
    int die4;
    int die5;

        //for loop incrementing by 1, until ROLLS
    for( numRolls = 0; numRolls < ROLLS; ++numRolls )
    {
        //insert randomized numbers from rollDie into dice 1-5
        die1 = rollDie();
        die2 = rollDie();
        die3 = rollDie();
        die4 = rollDie();
        die5 = rollDie();

        //printf output randomized dice into nice looking table
        printf("+-------+ +-------+ ------------------------|\n");
    printf("|       | |       |       |       |       |\n");
    printf("|   %d   | |   %d   |   %d   |   %d   |   %d   |\n", die1, die2, die3, die4, die5);
    printf("|       | |       |       |       |       |\n");
    printf("+-------+ +-------+ ------------------------|\n");
    }

}


int displayGameMenu()
{
    //declare int select
    int select = 0;

    //while loop
    while(select = 0)
    {
        //printf displays options
        printf("%d. Display Game Rules\n", RULES);
        printf("%d. Start a game of Yahtzee\n", GAME);
        printf("%d. Exit\n", EXIT);

        //scanf get user input, store in select
        scanf("%d", &select );

        //return select
        return select;
    }

}

int rollDie()
{
    //declare int dieValue
    int dieValue = 0;

    //sets dieValue equal to rand() with scaling factor 6 and shift factor 1
    dieValue = rand() % 6 + 1;

    //return dieValue
    return dieValue;
}

只关注你所问的问题,我发现两个问题

1 int input=显示游戏菜单;如果希望多次显示选项,则应在循环中

2 whileselect=0将0分配给select并计算为false,因此跳过循环内容。由于循环外没有返回,因此您有未定义的行为,这意味着程序可能会崩溃或返回任何值。由于您在编辑中更正了这一点,我希望您的程序第一次能够正常工作

这是一个精简版的程序,对我来说运行正常

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define RULES 1
#define GAME 2
#define EXIT 3

int displayGameMenu();

int main()
{
    int play = 1;
    while (play)
    {
        int input = displayGameMenu();
        switch (input)
        {
        case RULES:
            printf("RULES chosen\n");
            break;
        case GAME:
            printf("GAME chosen\n");
            break;
        case EXIT:
            printf("EXIT chosen\n");
            play = 0;
            break;
        default:
            printf("DEFAULT\n");
            break;
        }
    }
    return 0;
}

int displayGameMenu()
{
    int select = 0;

    while (select == 0)
    {
        printf("%d. Display Game Rules\n", RULES);
        printf("%d. Start a game of Yahtzee\n", GAME);
        printf("%d. Exit\n", EXIT);

        scanf("%d", &select);

        return select;
    }
    return 0;
}

我在这里进行了测试:结果与预期一致。

让我们了解问题所在

在该区块中:-

 int input = displayGameMenu();
 //initialize while loop controlled by play
    while(play == 1) {



            //list of cases to control function calls
            switch(input)
            {
                case RULES :
                    gameRules();
                    break;
                case GAME :
                    clearScreen();
                    displayRandomDice();
                    break;
                case EXIT :
                    printf("Thank you for playing!");
                    play = 0;
                    break;
                default  :
                    printf("Incorrect option, hit enter and try again");
                    char enter;
                    scanf("%c", &enter);
            }

    }
您正在调用displayGameMenu;一次,并将其返回值分配给输入。让我们假设它是1。现在,当play==1时,要退出这个循环,需要更改它的值

然后在下一行中,您将看到这个switchinput语句,根据我们的选择1,将执行case规则,然后它退出switch语句。注意:play的值现在并没有更新,所以play&input仍然是1。这就是它永远执行语句的原因

解决方案:

您必须在while循环内通过每次调用displayGameMenu函数来更新输入值

更新:这个问题被@Gerhardh编辑并回滚到原来的形式。因此,为了避免任何歧义,请查看OP问题下@Gerhardh的评论

现在对于这个问题的更新版本,程序中还有一个逻辑错误,在main中为whileplay=1,在displayGameMenu中为whileselect=0,
这应该通过应用等式==运算符而不是赋值=运算符来更正。如果未纠正,循环条件将始终为真。因此,循环的执行将是无限的。

whileplay=1{这分配了1来播放。也许你是想用==来测试相等性。这里同样,whileselect=0甚至不知道为什么第二个循环。@RetiredInja我试过了,同样的结果。我不知道为什么我应该在菜单函数中使用do/while循环,但这是规则的一部分。谢谢!我忘记了=只是分配变量.不要编辑你的错误!为了对任何人有用,问题中的错误不能被删除。回滚。
//initialize while loop controlled by play
    while(play) {
            int input = displayGameMenu();

            //list of cases to control function calls
            switch(input)
            {
                case RULES :
                    gameRules();
                    break;
                case GAME :
                    clearScreen();
                    displayRandomDice();
                    break;
                case EXIT :
                    printf("Thank you for playing!\n");
                    play = 0;
                    break;
                default  :
                    printf("Incorrect option, Press any key to continue...\n");
                    getchar();
            }
            putchar('\n');

    }