Need help getting objects out of different functions (C ++)

advertisements

I am fairly new to C++ and coding in general. I am trying to make just a basic little multiple choice type game for practice but I have run into a conundrum.

The program isn't outputting what I want it too. Here is the code:

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace std;

void sword(int damage);
void fists(int damage);

static int enemyHealth = 250;

int main() {
    srand(time(0));

    string SOF; //Abreveation for "Sword Or Fists"

    cout << "You will be fighting a mean bad guy. Are you using a sword, or your fists?\n";

    while (SOF != "sword" && SOF != "fists"){
        cout << "Please enter your choice of either 'sword' or 'fists': ";
        cin >> SOF;
    }

    cout << "Okay! Time to fight! \n";

    if (SOF == "fists") {
        void fists();
    }
    else if (SOF == "sword") {
        void sword();
    }
    else{ (NULL); }

    cout << "Congratulations! You have vanquished that foul beast!\n";

    system("pause");
}

//This is for when the user chooses 'sword'
void sword(int damage = rand() % 100 + 50) {
    while (enemyHealth > 0){
        cout << "You deal " << damage << " damage with your sharp sword. \n";
        enemyHealth -= damage;
    }
}

//This is for when the user chooses 'fists'
void fists(int damage = rand() % 10 + 4) {
    while (enemyHealth > 0){
        cout << "You deal " << damage << " damage with your womanly fists. \n";
        enemyHealth -= damage;
    }
}

The first part works fine, but when I enter my choice of either "fists" or "sword" the output is:

Okay! Time to fight!
Congratulations! You have vanquished that foul beast!

But I want it to output the damage being done with either fists or sword.

If I could get some help with that, it would be amazing. Thanks!


void fists(); is a declaration, not a call, change to fists(); and sword();

Other things to look at:

  • Default parameters are declared in function declaration before main (or just move whole functions there)
  • Default parameters in c++ are evaluated once, so all 'hits' will be the same in your code
  • Local variable names are usually not named in uppercase, SOF looks loke it is a #defined constant or such.