循环运行时错误
Looping runtime-error
我用c++做了一个非常简单的游戏。大部分工作正常,但每当我尝试重玩游戏时,该值都不会重置,而是从我上次离开的地方继续,这不是我希望它发生的情况。比如说,在我第一次尝试时,我击败了敌人,它的生命值为零,当我重试时,该值不是从 100 开始,而是从零开始。我真的想要解决这个问题。无论如何,这是我的代码("它很长):
#include <iostream>
#include <string>
using namespace std;
class Health{
private:
int health;
Health(const Health& cpy){
health = cpy.health;
}
public:
Health(){
health = 100;
}
void display_health(){
cout << "Health: " << health << endl;
}
Health& operator -=(int atk){
health -= atk;
return *this;
}
bool operator <(int compare){
return (health < compare);
}
bool operator >(int compare){
return !(this->operator<(compare));
}
bool operator &&(const int compare){
return (health && compare);
}
};
class Hero : public Health{
public:
void kick(){
cout << "Your kick damaged 45 health points" << endl;
}
void Punch(){
cout << "Your punch damaged 25 health points" << endl;
}
void Knife(){
cout << "Your kinfe damaged 60 health points" << endl;
}
void Tackle(){
cout << "Your tackle damaged 10 health points" << endl;
}
};
class Enemy : public Health{
public:
void Basic_hit(){
cout << "Enemy damaged you by 25 point" << endl;
}
};
int main(){
//local vaiables
string name;
int choice;
string ply_agn = "y";
//instance
Hero hero;
Enemy enmy;
//user interface
do{ //entire game repeats
cout << "Enter you character name: ";
getline(cin, name);
cout << "Your default health is : ";
hero.display_health();
cout << endl;
do{
//user interface
cout << "0.Kick - 45" << endl;
cout << "1.Punch - 25 " << endl;
cout << "2.Kinfe - 60" << endl;
cout << "3.Tackle - 10 " << endl;
//players choice
cout << "Which move you like to chose: ";
cin >> choice;
cout << endl;
//switch function
switch (choice)
{
case 0:
hero.kick();
cout << "Enemy ";
enmy -= 45;
enmy.display_health();
break;
case 1:
hero.Punch();
cout << "Enemy ";
enmy -= 25;
enmy.display_health();
break;
case 2:
hero.Knife();
cout << "Enemy ";
enmy -= 60;
enmy.display_health();
break;
case 3:
hero.Tackle();
cout << "Enemy ";
enmy -= 10;
enmy.display_health();
break;
default:
cout << "Invalid move! Try again" << endl;
break;
}
cout << endl;
//Enemy move
enmy.Basic_hit();
hero -= 25;
cout << "The enemy hit you with a basic hit. You recieved 25 damage" << endl;
cout << "Your ";
hero.display_health();
cout << endl;
} while (enmy > 0);
//winning condition
if (enmy < 0 && hero > 0) {
cout << "You won" << endl;
}
else if (hero < 0 && enmy > 0){
cout << "You lose" << endl;
}
else if (hero < 0 && enmy < 0){
cout << "This game is a draw" << endl;
}
} while (ply_agn == "y"); //This is where I think is messing up the game
//repeating condition
cout << "Would you like to play again: ";
cin >> ply_agn;
system("pause");
return 0;
}
谢谢你的时间
Hero hero;
Enemy enmy;
do{
之后,喜欢
...
string ply_agn = "y";
//user interface
do{ //entire game repeats
//instance
Hero hero;
Enemy enmy;
...
正如其他人所说,你需要把你的Hero
和Enemy
对象放在你的循环中,这样它们就会在游戏的每个循环后重新创建。
程序中的另一个错误是您将getline
与cin
混合在一起,因此在下一个循环中,您将无法为您的角色起一个新名字。为了避免这种情况,您需要在getline()
函数调用之前放置一个cin.ignore
,如下所示
cin.ignore (std::numeric_limits<std::streamsize>::max(), 'n');
getline(cin, name);
在这里,std::numeric_limits 在标头<limits>
中定义并使用,因为它是流的最大大小,因此它将忽略n
缓冲区中留下的尽可能多的换行符cin
。
最后的想法是,您需要在循环测试之前重复问题,如下所示
cout << "Would you like to play again: ";
cin >> ply_agn;
} while (ply_agn == "y");
否则ply_agn
将始终y
,用户将无法退出您的游戏:-)
您从未在游戏开始时显式重置生命值。如果您要通过这样的循环重置,游戏的第一步应该是重置Hero
和Enemy
。只需移动到将它们声明为do while
循环中的第一行的位置即可。
相关文章:
- 删除指向指针的指针是运行时错误吗
- c++中的指针和运行时错误
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 在同一模拟中使用静脉和静脉_ inet内容时出现运行时错误
- 对单向链表进行排序时出现运行时错误
- 为什么此代码存在运行时错误?
- 你能解释一下什么运行时错误是如何解决它的吗?
- 为什么会出现 gettnig 运行时错误:加载类型为"_Bit_type"(stl_bvector.h) 的空指针?
- 为什么程序在 c++ 中迭代 emtpy 向量时会抛出运行时错误
- 运行时错误:引用绑定到类型为"int"的空指针
- 为什么当 vector 为空时会显示运行时错误?
- 类方法 - 数据结构中 For 循环的运行时错误
- C++代码在 for 循环的条件下给出运行时错误,而如果它被具有相同意义的代码替换,则编译正确
- 在循环中使用lower_bound函数会给出运行时错误
- 循环运行时错误
- 循环和运行时错误中的C 变量声明
- 尝试拆分循环链表.显示输出但崩溃.在 IDEone 中显示运行时错误
- 第15次循环后出现链表运行时错误
- 我想在我的代码中实现一个双重循环链表 - 运行时错误
- 我认为做而正在进入无限循环.或数组.(运行时错误)