Garmaine Staff asked 2 years ago
Closed. This question is off-topic. It is not currently accepting answers.

Want to improve this question? Update the question so it's on-topic for Stack Overflow.

Closed 8 hours ago.

I recently created a text based battle simulator in python. It was cool and all, but i am more interested in c++, so i wanted to port it into that language, in the form of a header file that i already used for other things. However, i have no idea how the header system works in c++. I tried removing the dash symbol from the name and replacing it with an underscore, but that didn't work. I even tried putting the code into a namespace in the header, but it said that some of the code that i put in the namespace didn't belong in the namespace when i was trying to compile the test program. In other words, i am trying to include a header file in a c++ program, but I don't know how to do so, as everything i try won't work. Also, when it looks through the header file for errors, it takes a look at one of the classes and notices an integer function. It then tells me that it can't convert type int to type int. Why that happened, it makes no sense to me.

PS: I am using mingw32 on mingw64 gcc to compile this program. If anyone has an answer or might have an answer, i'll be sure to reply (at some point). Also, if anybody needs reference code, then i might or might not be able to give it to you.

EDIT: By port i mean rewriting the code in c++

UPDATE: I've reached my question limit so i had to squeeze this one in. How am i able to break out of a while loop? gcc gives me an error that tells me that break isn't meant for loops and switch statements.

UPDATE: This is the code for a header file that includes the code for the battle function. I don't think it is much of a problem with the function though:

string Battle(string playerName, string enemyName, int playerHealth, int enemyHealth, int playerSpecial, int enemySpecial)
{

    int old_ehp = enemyHealth;
    int old_php = playerHealth;

    cout << "A challenger aproaches. " << enemyName << " wants to fight!" << endl;
    cout << "Let the battle begin." << endl;

    do
    {

        // players turn text
        cout << playerName << "'s turn." << endl;
        cout << "(1). Attack (2). Special (3). Give up" << endl;

        cout << "> ";

        // attack info

        int attack;
        cin >> attack;

        // Check if int(attack) equals 1
        if(attack == 1)
        {

            cout << "Player attacks!" << endl;

            // Define critical hit
            int critical = rand() % 4;

            if(critical == 3)
            {

                cout << "Critical hit!" << endl;
                enemyHealth -= 4;

            }
            enemyHealth -= 3;

        } else if(attack == 2)
        {

            cout << "Player uses special attack!" << endl;
            int critical = rand() % 6;
            if(critical == 5)
            {

                cout << "Critical hit!!!" << endl;
                enemyHealth -= 5;

            }
            enemyHealth -= playerSpecial;

        }

        cout << enemyName << "'s turn." << endl;
        cout << enemyName << " has decided what attack he/she will use." << endl;

        attack = rand() % 6;

        if(attack != 5)
        {

            cout << enemyName << " attacks!" << endl;

            int critical = rand() % 4;

            if(critical == 3)
            {

                cout << "Critical hit!" << endl;
                playerHealth -= 4;

            }
            playerHealth -= 3;

        } else {

            cout << enemyName << " uses special attack!" << endl;

            int critical = rand() % 6;

            if(critical == 5)
            {

                cout << "Critical hit!" << endl;
                playerHealth -= 5;

            }
            playerHealth -= enemySpecial;

        }

        int healer = rand() % 20;

        if(healer == 0 or healer == 1)
        {

            cout << playerName << " found HP revive!" << endl;
            playerHealth += 5;

        } else if(healer == 19) {

            cout << playerName << " found HP super revive!" << endl;
            playerHealth = old_php;

        }

        healer = rand() % 40;
        if(healer == 0 or healer == 1)
        {

            cout << enemyName << " found HP revive!" << endl;
            enemyHealth += 5;

        } else if(healer == 39)
        {

            cout << enemyName + " found HP super revive!" << endl;
            enemyHealth = old_ehp;

        }

        int magicFlute = rand() % 100;

        if(magicFlute == 72)
        {

            cout << playerName << " obtained magic flute!" << endl;
            cout << playerName << " uses the magic flute to one hit K.O. " << enemyName << "!!!" << endl;
            enemyHealth = 0;

        }

        if(playerHealth < 0){
            cout << playerName << "'s health is 0" << endl;}
        else{
            cout << playerName << "'s health is " << playerHealth << endl;
        }
        if(enemyHealth < 0)
        {

            cout << enemyName << "'s health is 0" << endl;

        } else {

            cout << enemyName << "'s health is " << enemyHealth << endl;

        }

    } while(enemyHealth > 0 or playerHealth > 0);

    if(playerHealth <= 0)
    {

        cout << "You lost the battle." << endl;
        break;

    }
    else if(enemyHealth <= 0)
    {

        cout << "You won the battle." << endl;
        break;

    }

};

My problem is that i don't know how to include this into a test program and use the function inside of the header file.