需要帮助从不同的功能输出内容(C++)
Need help outputting things from different functions (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!
但我希望它输出用拳头或剑造成的伤害。
如果我能得到一些帮助,那就太神奇了。谢谢!
void fists();
是一个声明,而不是一个调用,更改为fists();
和sword();
其他注意事项:
- 默认参数在
main
之前在函数声明中声明(或者只是将整个函数移动到那里) - c++ 中的默认参数只计算一次,因此代码中的所有"命中"都是相同的
- 局部变量名称通常不以大写形式命名,
SOF
看起来它是一个#define
d 常量之类的。
调用函数,不要写void fists();
,只是
fists();
(您拥有的是一个声明,在这里没有有用的效果,而不是调用。
相关文章:
- 递归函数计算序列中的平方和(并输出过程)
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 请解释"函数1(p1,p2,p3);"的输出
- C++:将控制台输出存储在宏中更好吗
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 为什么我的代码在输出中增加了93天
- 如何从void函数输出字符串
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- AES加密到解密未正确输出
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- C++格式化输出问题
- 将值从二维数组输出到文本文件
- 集合上的输出迭代器:assign和increment迭代器
- 在while循环中输入带有std::cin的字符串后,控制台会输出大量胡言乱语
- 为什么rk4.do_step不输出C++中的初始条件
- 输出没有重复元素的动态数组(收缩数组)C++
- 如何根据单词在文本中出现的概率输出单词