我在程序上找不到错误 (C++)
I can't find the error on my program (C++)
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
void divider();
int main()
{
int cstats, choice;
int rhp, hp, atk, def, matk, mdef, dmg, mdmg, agi, magi;
divider();
cout << "Kill the Zombie! (Text-Based Game)n";
divider();
cout << "Please choose your specialty: ";
cout << "[1] Offense [2] Magic [3] Defense [4] Speedn";
do{cin >> cstats; }
while(cstats > 4 || cstats < 1);
{
switch(cstats)
{
case 1:
atk = 15;
def = 8;
agi = 6;
matk = 5;
mdef = 5;
magi = 5;
break;
case 2:
atk = 5;
def = 5;
agi = 5;
matk = 15;
mdef = 7;
magi = 6;
break;
case 3:
atk = 7;
def = 15;
agi = 5;
matk = 1;
mdef = 14;
magi = 3;
break;
case 4:
atk = 7;
def = 4;
agi = 15;
matk = 3;
mdef = 4;
magi = 14;
break;
}
if(cstats == 1)
{
cout << "You have chosen Offensen";
}
if(cstats == 2)
{
cout << "You have chosen Magicn";
}
if(cstats == 3)
{
cout << "You have chosen Defensen";
}
if(cstats == 4)
{
cout << "You have chosen Speedn";
}
}
srand((unsigned)time(0));
rhp = rand()%500 + 200;
hp = rand()%500 + 200;
while(hp > 0 || rhp > 0 )
{
cout << "What do you want to do? [1] Normal Attack [2] Magic Attack [3] Defend [4] Dodgen";
do{cin >> choice; }
while(choice > 4 || choice < 1);
{
switch(choice)
{
case 1:
atk = rand()%20+10;
break;
case 2:
matk = rand()%20+10;
break;
case 3:
atk = rand()%5+1;
def = rand()%10+10;
break;
case 4:
agi = rand()%10+10;
break;
}
if(choice == 1)
{
mdmg = ((0.10 * def) / (1 + 0.10 * def)) * 100;
}
if(choice == 2)
{
mdmg = ((0.20 * mdef) / (1 + 0.20 * mdef)) * 100;
}
if(choice == 3)
{
dmg = ((0.06 * def) / (1 + 0.06 * def)) * 100;
}
if(choice == 4)
{
mdmg = ((0.10 * agi) / (1 + 0.20 * agi)) * 100;
}
rhp = rhp - mdmg;
cout << "You did " << mdmg << "damage to the zombie!n";
cin.get();
if(rhp <1)
{
cout << "You killed the Zombie! Congratulations, You won with " << hp << "hp left.n";
cin.get();
system("pause>0");
return 0;
}
cout << "The Zombie now has " << rhp << "hp left.n";
dmg = ((0.06 * def) / (1 + 0.06 * def)) * 100;
dmg = ((0.10 * mdef) / (1 + 0.10 * mdef)) * 100;
dmg = ((0.05 * agi) / (1 + 0.05 * agi)) * 100;
if(dmg < 0)
{
dmg = 0;
}
hp = hp - dmg;
cout << "The Zombie hit you for " << dmg << " damage.n";
if(hp < 1 )
{
cout << "You died. The Zombie still has " << rhp << "hp left.n";
cin.get();
system("pause>0");
return 0;
}
cout << "You now have " << hp << " hp left.n";
}
}
}
void divider()
{
cout << "*************************************n";
}
这是我程序的全部代码,我总是收到一个错误,说"运行时检查失败 #3 - 变量'mdmg'在没有初始化的情况下被使用。我哪里做错了?我找不到它。我需要在 3 小时内完成此操作。:(
我猜你的问题在这里:
if(choice == 3)
{
dmg = ((0.06 * def) / (1 + 0.06 * def)) * 100;
rhp = rhp - mdmg;
}
如果您在任何其他选择之前调用它,则在从 rhp 中减去之前不会初始化 mdmg。
编辑:
尽管删除第二个语句是个好主意,但您仍然会遇到相同的问题,即 mdmg 未在 if(choice ==3)
块中初始化。 你的意思是写吗
if(choice == 3)
{
mdmg = ((0.06 * def) / (1 + 0.06 * def)) * 100;
}
(DMG改为MDMG)
问题是您尝试使用mdmg
而不按照错误告诉您的那样初始化它。
cout << "You did " << mdmg << "damage to the zombie!n";
您可以在各种if
语句中为mdmg
赋值,但由于没有mdmg
的默认值(您可能无法输入将值分配给mdmg
的if
块之一),因此在尝试打印时会出现错误。
要么在声明它时初始化它(int mdmg = 0;
),要么在其中一个if
语句中没有其他方法来分配默认值。
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 错误:未在此范围内声明'reverse'