如果用户选择特定函数

时间:2015-10-16 15:57:09

标签: c function loops pointers do-while

嗨,我对问题的帮助不大!

我有一个包含循环的主类。 在这个循环中,用户可以调用不同的函数来执行不同的操作,但我希望如果用户选择某个函数,它会显示一条消息并且循环结束。

这是我的代码:

#include <stdio.h>

void rest_at_inn();
void train();
void battle_demon_lord();
void Quit_Game();

int main() {

    //Declare variable and pointer
    float days_remaining = 8;
    int current_hp = 10;
    int hp_remaining = 10;
    int MAX = 10;
    int experience = 0;
    int selection;

    //Loops a menu that asks the user to make a selectio and repet it until either days or HP are less or equal to zero
    do {
        printf("Days remaining: %f, HP: %d, EXP: %d\n\n", days_remaining, hp_remaining, experience);
        printf("1 - Rest at Inn\n");
        printf("2 - Train\n");
        printf("3 - Fight the Demon Lord\n");
        printf("4 - Quit Game\n\n");
        printf("Select: \n");
        scanf("%d", &selection);
        printf("\n");

        //Execute the selection
        switch (selection) {
            case 1:
                rest_at_inn(&days_remaining, &hp_remaining, &MAX);
                break;
            case 2:
                train(&days_remaining, &hp_remaining, &experience);
                break;
            case 3:
                battle_demon_lord(&current_hp);
                break;
            case 4:
                Quit_Game();
                break;
        }

    } while (days_remaining > 0 && current_hp > 0);

    return 0;

}

//This function reduces the days by 1 and resets the HP to 10

void rest_at_inn(float *days_remaining, int *hp_remaining, int *MAX) {

    *hp_remaining = 10;
    *days_remaining -= 1;
    if (*days_remaining > 0)
        printf("You rested up the at the inn\n\n");
    else /*(*days_remaining<=0)*/
        printf("Game Over");

}

//This function reduces the days by 0.5 and the HP by 2 and adds 10 to the experience

void train(float *days_remaining, int *hp_remaining, int *experience) {

    *experience += 10;
    *hp_remaining = *hp_remaining - 2;
    *days_remaining = *days_remaining - 0.5;
    printf("You did some training!\n\n");

}

//This function sets the HP to 0 and prints a string

void battle_demon_lord(int *current_hp) {

    *current_hp = 0;
    printf("He's too strong!\n\n");
    printf("Game Over!");
}

// This function prints a string

void Quit_Game() {

    printf("Good bye!\n\n");

}

我认为它应该是do_while中的一个参数,但没有任何对我有用。

4 个答案:

答案 0 :(得分:4)

您需要在用户选择4时添加退出条件。

你可以这样做:

} while (days_remaining>0 && current_hp>0 && selection != 4);

或者这个:

case 4 :
    Quit_Game();
    return 0;

或者这个:

void Quit_Game()  {

    printf ("Good bye!\n\n");
    exit(0);

}

答案 1 :(得分:2)

如果switch是循环的最后一个语句,请在mustQuit / do循环的do之前添加while标志变量,并且将其设置为零。当最终用户选择退出选项时,请将mustQuit设置为1。将!mustQuit条件添加到循环中。

int mustQuit = 0;
do {
    ... // some statements here
    switch (selection) {
    ...
    case 4 :
        mustQuit = 1;
        Quit_Game();
        break;
    }
} while (!mustQuit && days_remaining > 0 && current_hp > 0);

如果您在switch之后有更多陈述,则中间带有条件break的替代方案可能更为理想:

do {
    int mustQuit = 0;
    ... // some statements here
    switch (selection) {
    ...
    case 4 :
        mustQuit = 1;
        Quit_Game();
        break; // this breaks the switch
    }
    if (mustQuit) {
        break; // this breaks the loop
    }
    ... // More statements here
} while (days_remaining > 0 && current_hp > 0);

答案 2 :(得分:0)

我希望你想要逃避case 4中的循环:

尝试使用goto

    case 4 :
        Quit_Game();
        goto out;
        break;
    }

} while (days_remaining>0 && current_hp>0);
out:
return 0;

答案 3 :(得分:0)

(1)如果你想退出do-while循环,你可以添加一个布尔变量,所以当它输入最后一个选项时,它将是假的,如下所示:

bool quit = false;

(...)

case 4 :
    quit = true;
    printf("Good bye!\n\n");
    break;

并在do-while的条件下添加值:

while (days_remaining>0 && current_hp>0 && !quit);

因此,它将退出do-while循环并继续代码。

(2)如果你想完成程序,添加一个返回0并忘记布尔值,如下所示:

case 4 :
    printf("Good bye!\n\n");
    return 0;

请记住:如果您是在基于C的代码上执行此操作,则它不会支持保留字,例如truefalse。您必须使用0表示false,其他数字表示true,最好是1。