需要帮助输出不同功能的东西(C ++)

时间:2016-01-16 22:01:18

标签: c++

我对C ++和编码很新。我试图为练习制作一个基本的小型多选类型游戏,但我遇到了一个难题。

该程序也没有输出我想要的东西。这是代码:

#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;
    }
}

第一部分工作正常,但当我选择"fists""sword"时,输出为:

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

但我希望它输出用拳头或剑完成的伤害。

如果我能得到一些帮助,那就太棒了。谢谢!

2 个答案:

答案 0 :(得分:3)

void fists();是声明,而不是来电,更改为fists();sword();

其他要注意的事项:

  • 默认参数在main之前的函数声明中声明(或只是在那里移动整个函数)
  • c ++中的默认参数被评估一次,因此所有&#39;命中&#39;将在您的代码中使用相同的内容
  • 本地变量名称通常不以大写字母命名,SOF看起来像是#define d常量等。

答案 1 :(得分:1)

要拨打此功能,请不要写void fists();,只需

fists();

(你所拥有的是一个声明,这里没有任何有用的效果,而不是一个电话。)