2

Sorry for being a noob. How do I return to the line of code just after where attackRat() is called? After the rat is dead I want the program to come to here, how do I do that? Thanks and sorry if I'm being stupid.

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

    }
}
Ken White
  • 123,280
  • 14
  • 225
  • 444
  • 4
    You should grab a [good book](http://stackoverflow.com/q/388242/1782465) and look up concepts like loops and the `return` statement. – Angew is no longer proud of SO Feb 04 '15 at 20:12
  • 2
    Unless you intend to open the backpack automatically after you attack the rat, you will probably want a `break` statement after `attackRat()`. Generally, it's good practice to have a `break` statement at the end of each case within a switch unless you have a very good reason for not doing so. – Logicrat Feb 04 '15 at 20:16
  • 3
    Aside from the question, you almost certainly want a `break;` at at the end of each `case` section. Without that, if the user chooses to attack the rat, they will *also* open a backback. – Josh Feb 04 '15 at 20:16
  • Thank you for the help but I still don't understand what is wrong with the programme. – Elliot Morgan Feb 04 '15 at 20:44
  • Only call srand ONCE in your program. – Neil Kirk Feb 04 '15 at 21:26
  • 1
    I rolled back your last edit. **Do not** edit your title to indicate it's *Still Unsolved* (or *Solved*, for that matter). If your question is solved, accept the answer that provided the solution. If it is unsolved, simply *don't accept an answer*. Editing the title to show the status is inappropriate. – Ken White Feb 04 '15 at 21:28

2 Answers2

4

Just return from attackRat():

// Does something go here to return it to the bit I want?
return;

Then once you're back in ratCave():

// 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.";
gknicker
  • 5,509
  • 2
  • 25
  • 41
4

Instead of calling attackRat() from inside attackRat itself to do a loop use a normal loop with:

for(;;) {
   ... code to repeat forever ...
}

then you can use return to go back to who called the function

6502
  • 112,025
  • 15
  • 165
  • 265