Question On Classes

I'm making a simple game and I have a battle class with the battle system in it. If the player's health reaches 0 I want it to goto mainMenu; but that's in the Main.cpp. How can I make it go there? Thanks
Last edited on
closed account (zb0S216C)
Without proper details, it's kinda hard to say. Here's a possible solution:

1
2
3
4
5
6
7
while( /* Condition */ )
{
    if( Player.Health == 0 )
        Game.CreateMenu( );

    Enemy.TakeTurn( );
}
It's not clock-efficient, but it's a start.

Wazzak
Last edited on
Yeah, sorry I left out some important stuff, but here's a part of the code in the Battle class.
When it gets to line 43 after they lose, I want it go to mainMenu, which is in the main.cpp and if I try it as it is now I get
error: label 'mainMenu' used but not defined


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
int bonus = 1+(rand()%5);

    cin >> battle;

    if (battle == 1)
    atk = baseAtk + bonus;
    enemyHealth = enemyHealth - atk;
    Beep(646,20);
    Beep(810,30);
    cout << "You did " << atk << " damage!\n";
    cin.ignore();
    cin.get();

    if (enemyHealth <= 0)
    goto win;

    int eBonus = 1+(rand()%5);
    eAtk = enemyAtk + eBonus;
    baseHealth = baseHealth - eAtk;
    Beep(656,20);
    Beep(327,30);
    system("color c");
    system("color 4");
    system("color c");
    system("color 4");
    system("color c");
    system("color c");
    system("color 4");
    system("color c");
    system("color 0f");
    cout << "The enemy did " << eAtk << " damage!\n\n";
    cin.get();

    if (baseHealth <= 0 ){
    system("color 4");
    Beep(334,30);
    Beep(296, 25);
    Beep(252, 23);
    Beep(177, 20);
    Beep(143, 27);
    Beep(102, 60);
    cout << "You died!";
    goto mainMenu;

    }


    }while (enemyHealth >= 1);

        win:
        Beep(634, 37);
        Beep(679, 35);
        Beep(732, 40);
        Beep(900, 100);
        Beep(978, 400);

        int winExp = 1+(rand()%20);
        exp = exp + winExp;
        int winGold = 28+(rand()%30);
        gold = gold + winGold;
        cout << "You won!\n\n";
        cout << "You got " << winExp << " exp!\n\n";
        cout << "You got " << winGold << " gold!\n\n";
        cin.get();
        system("cls");
Last edited on
closed account (zb0S216C)
Never use Label: ... goto. Functions provide a much better solution. Instead of throwing everything in main( ), use a separate function, such as this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
enum GameState
{
    GameState_PlayerDied,
    GameState_PlayerLived
};

void MainMenu( )
{
    // Display the menu...
    ...
    return;
}

GameState GameCycle( )
{
    // User input.
    
    // The player died.
    if( Player.Health == 0 )
        return GameState_PlayerDied;

    return GameState_PlayerLived;
}

int main( )
{
    while( true )
    {
        switch( GameCycle( ) )
        {
            case GameState_PlayerDied:
                MainMenu( );
                break;

            default:
                continue;
        }
    }
}

Label: ... goto just complicates things.

Wazzak
Last edited on
I'm pretty new to C++ and I haven't used some of these functions, so bear with me. Since this is all in the Battle class what do I need to put above the menu code that's in the main.cpp so it knows where to go?
closed account (zb0S216C)
BusinessSloth wrote:
Since this is all in the Battle class what do I need to put above the menu code that's in the main.cpp so it knows where to go? (sic)

Could you clarify this, please?

Wazzak
Say you use goto mainMenu; Above the main menu code you would have mainMenu: so it know where where to go. In the code that you posted, where would the code for the main menu go? Basically, what would be the mainMenu: of this way, if the two can be compared.
closed account (zb0S216C)
As I previously said, never use Label: ... goto. It complicates code, as you've shown. Do you know how to create functions?

Wazzak
Last edited on
Oh, I understand it now! I was pretty tired when you posted that bit of code, but looking at your post now it makes perfect sense and it solved my problem. Thank you!
Topic archived. No new replies allowed.