首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在函数被调用的地方返回到行?

如何在函数被调用的地方返回到行?
EN

Stack Overflow用户
提问于 2015-02-05 04:09:26
回答 2查看 104关注 0票数 2

很抱歉我是个菜鸟。如何在调用attackRat()之后返回代码行?老鼠死后,我想让程序转到这里,我该怎么做呢?谢谢,如果我太愚蠢了,我很抱歉。

代码语言:javascript
复制
void ratCave() {

    location = "rat cave";
    system("cls");
    cout << "\n You arrive at the cave where two rats greet you at the entrance." << endl;
    cout << "\n What would you like to do?" << endl;
    cout << "\n 1. Attack the rats" << endl;
    cout << "\n 2. Open backpack" << endl;
    cout << "\n> ";
    cin >> input;
    switch (input) {

        case 1:
        attackRat();    
        // I want the program to come here once the rat is dead.

        case 2:
        backpack();

    }
}

void attackRat() {

    srand (time(NULL));

    damage = rand() % 10 + 10;
    ratDamage = rand() % 5 + 5;

    if (start == 1) {

        ratHealth = rand() % 20 + 30;
        turn = rand() % 2;

        if (turn == 1) {
            system("cls");
            cout << "\n The rat have the first turn." << endl;
            cout << "\n ";
            system("pause");

            health = health - ratDamage;
            system("cls");
            cout << "\n The rat attacks you for " << ratDamage << " health!" << endl;
            cout << "\n ";
            system("pause");    
        }

        else {
            system("cls");
            cout << "\n You have the first turn." << endl;
            cout << "\n ";
            system("pause");    
        }
    }

    start = 0;

    system("cls");
    cout << "\n Your health: " << health << endl;
    cout << "\n Rat health:  " << ratHealth << endl;
    cout << "\n What do you do?" << endl;
    cout << "\n [1] Attack rat" << endl;
    cout << "\n [2] Use a potion" << endl;
    cout << "\n> ";
    cin >> input;
    switch (input) {

        case 1:
        ratHealth = ratHealth - damage;
        system("cls");
        cout << "\n You attack the rat for " << damage << " damage!" << endl;
        cout << "\n ";
        system("pause");

        if (ratHealth < 1) {
            ratHealth = 0;
            system("cls");
            cout << "\n You killed the rat!" << endl;
            cout << "\n ";
            system("pause");
            // Does something go here to return it to the bit I want?
        }

        health = health - ratDamage;
        system("cls");
        cout << "\n The rat attacks you for " << ratDamage << " damage!" << endl;
        cout << "\n ";
        system("pause");

        if (health == 0) {
            system("cls");
            cout << "\n You died!" << endl;
            cout << "\n ";
            system("pause");
            exit(0);    
        }

        case 2:
        attackRat();

    }
}
EN

回答 2

Stack Overflow用户

发布于 2015-02-05 04:13:26

刚从attackRat()返回

代码语言:javascript
复制
// Does something go here to return it to the bit I want?
return;

当你回到ratCave()的时候

代码语言:javascript
复制
// I want the program to come here once the rat is dead.
cout << "\nYup, the rat's dead and we're back in the rat cave.";
票数 4
EN

Stack Overflow用户

发布于 2015-02-05 04:15:31

不是从attackRat内部调用attackRat()来执行循环,而是使用一个普通的循环:

代码语言:javascript
复制
for(;;) {
   ... code to repeat forever ...
}

然后,您可以使用return返回调用该函数的用户

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28330521

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档