C程序应在执行前返回默认情况吗?

问题描述 投票:0回答:1

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

我尝试在达到大小写之前调用displayGameMenu,它仍然会显示默认大小写。我尝试制作一个与displayGameMenu相等的int变量,然后将其传递到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;
}
c function loops switch-statement case
1个回答
0
投票

仅关注您要问的问题,我看到两个问题。

1] int input = displayGameMenu();应该在循环内,如果您希望多次显示选择。

2] while(select = 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;
}

我在这里进行了测试:https://ideone.com/yv2WZm,其行为符合预期。

© www.soinside.com 2019 - 2024. All rights reserved.