Search code examples
c++intswitch-statement

Interger not updating value in switch function in C++


Kinda of a complete beginner here.

I am starting in C++, and programming in general, so, I'm trying to do a rough menu for a text RPG combat game, for practicing conditionals.

But, the values aren't updating themselves. Here is the code:

int main() {

    cout << "Wild Ogre attacked!" << endl << endl;
    int ogreHP = 350;
    int HP = 100;
    int Stamina = 100;
    int Magicka = 100;

    while (ogreHP > 1) {
    cout << "HP: " << HP << endl;
    cout << "Stamina: " << Stamina << endl;
    cout << "Magicka: " << Magicka << endl << endl;

    cout << "Ogre HP: " << ogreHP << endl << endl;

    cout << "What are you going to do? " << endl;
    cout << "1.\tAttack." << endl;
    cout << "2.\tMagicka." << endl;
    cout << "3.\tTactics." << endl;
    cout << "4.\tBag." << endl;
    cout << "5.\tRun." << endl;
 
    int menu_input;
    cin >> menu_input;

    switch (menu_input) {
        case 1: {
            
            cout << "1.\tLight Attack." << endl;
            cout << "\t\tDeals 5 points of damage. No Stamina cost." << endl << endl;
            cout << "2.\tHeavy Attack" << endl;
            cout << "\t\tDeals 20 points of damage. Stamina Cost of 10 points." << endl << endl;
            
            int att_input;
            cin >> att_input;
            
            switch (att_input) {

                case 1: {

                    cout << "You dealt 5 points of damage!" << endl << endl;
                    ogreHP= - 5;
            
                    break;
                }
                case 2: {

                    cout << "You dealt 20 points of damage!" << endl << endl;
                    Stamina= - 10;
                    ogreHP= - 20;

                    break;
                }

Thanks in advance!


Solution

  • When you write this syntax:

    Stamina = - 10;
    ogreHP = - 20;
    

    You actually assigns -10 and -20 to Stamina and ogreHP respectively, they're not called 'reducing' here. Rather than that, if you could've written the following:

    Stamina -= 10; // Stamina = Stamina - 10; -> previous - 10 = now
    ogreHP -= 20;  // ogreHP = ogreHP - 20;   -> previous - 20 = now
    //     ^^ is called 'assignment operator'
    

    The problem will be solved. The same stuff you did in case 1 in the code.