C++ 在Linux VSCode(gcc)和Windows Clion(visual c+;+;)上,同一代码的输出不同

C++ 在Linux VSCode(gcc)和Windows Clion(visual c+;+;)上,同一代码的输出不同,c++,gcc,visual-studio-code,visual-c++,clion,C++,Gcc,Visual Studio Code,Visual C++,Clion,我用VSCode(gcc)为我的项目制作了一个基于文本的游戏。主要的问题是VSCode给了我奇怪的输出,而Clion(visualc++)给了我正确的输出 例如,在VSCode中,我得到的伤害是负数,而在Clion中,我得到的伤害是正确的。 我希望我提供的代码将帮助您找到这种奇怪行为的原因 我想知道如何解决这个问题,以及如何在不久的将来避免它。先谢谢你 Main.cpp #include <iostream> #include <vector> #include <

我用VSCode(gcc)为我的项目制作了一个基于文本的游戏。主要的问题是VSCode给了我奇怪的输出,而Clion(visualc++)给了我正确的输出

例如,在VSCode中,我得到的伤害是负数,而在Clion中,我得到的伤害是正确的。 我希望我提供的代码将帮助您找到这种奇怪行为的原因


我想知道如何解决这个问题,以及如何在不久的将来避免它。先谢谢你

Main.cpp

#include <iostream>
#include <vector>
#include <ctime>
#include <string>
#include <algorithm>
#include "mob.h"


using namespace std;
int main()
{
    //Random seed
    srand(time(nullptr));

    //Player Initialization



    //Mobs Initialization
    mob Skeleton{rand()%51+50, rand()%21+30, rand()%11, "Skeleton", rand()%11+10};
    mob Zombie{rand()%101+100, rand()%11+10, rand()%21, "Zombie", rand()%11};
    mob Spider{rand()%21+30, rand()%11+20, rand()%11,  "Spider", rand()%21+50};
    mob Creeper{rand()%16+5, rand()%51+50, 0, "Creeper", rand()%6};

    //Array of mobs
    vector <mob> mobs;
    mobs.push_back(Skeleton);
    mobs.push_back(Zombie);
    mobs.push_back(Spider);
    mobs.push_back(Creeper);

    //Fight
    int CurrentAttack = 0, n;

    while (1 < mobs.size())
    {
        //Mobs order output
        cout << endl << mobs[0].name << endl;
        cout << "Choose who to attack:" << endl;
        tryAgain: //In case if user typed wrong number
        for (int q = 1; q < mobs.size(); q++)
        {
            cout << q << " - " << mobs[q].name << endl;
        }
        //Choose who to attack
        cin >> n;
        switch (n)
        {
            case 1:
            {
                mobs[0].Attack(CurrentAttack);
                mobs[1].TakeHit(CurrentAttack);
                mobs[1].Return();

                break;
            }
            case 2:
            {
                if (n > mobs.size()-1)
                {   printf("This enemy is already dead");
                    goto tryAgain;
                }
                mobs[0].Attack(CurrentAttack);
                mobs[2].TakeHit(CurrentAttack);
                mobs[2].Return();

                break;
            }
            case 3:
            {
                if (n > mobs.size()-1)
                {
                    cout << "This enemy is already dead" << endl;
                    goto tryAgain;
                }
                mobs[0].Attack(CurrentAttack);
                mobs[3].TakeHit(CurrentAttack);
                mobs[3].Return();

                break;
            }
            default:
                goto tryAgain;
        }

        if (mobs[n].Health() == 0)
        {
            //Remove dead mob
            rotate(mobs.begin(),mobs.begin()+n+1,mobs.end());
            mobs.pop_back();
        }
        else
        {
            //Rotate Vector for different mob to attack
            rotate(mobs.begin(),mobs.begin()+1,mobs.end());
        }

    }
    cout << "⣿⣿⣿⣿⡿⠟⠛⠛⠛⠛⠉⠉⠙⠛⠛⠿⢿⣿⣿⣿⣿⣿⣿⣿⣿⡿⠿⠟" << endl << "⣿⣿⣯⣥⣤⣶⣶⣶⣶⣿⣿⣿⣿⣿⣿⣷⣾⣿⣿⣿⣿⣿⣿⣿⣏⣀⣀⣀⡀" << endl << "⣿⣿⣿⣿⣿⣿⣿⡿⠿⠛⠛⠻⠿⠟⠉⠉⠉⢻⣿⣿⣿⡿⠟⠋⣡⣼⣿⣿⣿⡄" << endl
    << "⣿⣿⣿⣟⣭⣤⣶⣶⣿⣿⠃⠀⠀⢀⣀⣤⣶⣿⣿⣿⣿⡅⡀⢀⣩⣤⣤⠀" << endl << "⣿⣿⣿⣿⣿⣿⣛⡛⠛⠛⠛⢋⣩⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣛⠛⠛⠓⠠" << endl << "⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣦⣤⣤⣤⣦" << endl
    << "⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⡇" << endl << "⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⠿⠿⠿⠿⠿⢿⡿⢿⣿⣿⣿⠃" << endl << "⠿⠿⠿⠿⢿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣥⣄⣀⣀⠀⠀⠀⠀⠀⢰⣾⣿⣿⠏" << endl
    << "⠀⠀⠀⠀⠀⠀⠉⣩⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣿⣶⣤⣜⡻⠋" << endl << "⣰⣾⣷⣶⣿⣾⣖⣻⣿⣿⡿⣿⣿⣿⣿⠿⠿⠟⠛⠛⠛⠋⠉⠉⢉⡽⠃" << endl << "⣿⣿⣿⣿⣿⣿⡉⠉⠉⠛⠛⠛⠉⠁⠀⠀⠀⠀⠀⠀⠀⠀⡤⠚⠉" << endl
    << "⠛⠛⣿⣿⣿⣿⣿⣿⣿⠉⠛⢶⣶⣄⠀⠀⠀⠀⠀⠀⠀⠀⡇" << endl << "⠠⣾⣿⣿⣿⣿⣿⠿⠟⠃⠀⠀⠀⠈⠲⣴⣦⣤⣤⣤⣶⡾⠁" << endl << "⠄⠈⠉⠻⢿⣿⣿⡀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠛⠛⠛⠉⠀" << endl;

    cout << endl << mobs[0].name << " has survived!" << endl;
    return 0;
}

#include "mob.h"

using namespace std;

//Attack function
void mob::Attack(int& CurrentAttack) {
    int n;
    cout << "Choose what attack to use:\n1 - Strong Attack;\n2 - Simple Attack;\n3 - Critical Strike;\n";
    cin >> n;
    switch (n) {
        case 1:
            StrongAttack(CurrentAttack);
            break;
        case 2:
            SmallAttack(CurrentAttack);
            break;
        case 3:
            Crit(CurrentAttack);
            break;
        default:
            SmallAttack(CurrentAttack);
            break;
    }
}

//Types of Attack
void mob::StrongAttack(int& CurrentAttack)
{
    this -> attack = damage*2;
    mob::GiveAttack(CurrentAttack);
}
void mob::SmallAttack(int& CurrentAttack)
{
    this -> attack = damage;
    mob::GiveAttack(CurrentAttack);
}
void mob::Crit(int& CurrentAttack)
{
    if (rand() % 2)
    {
        this -> attack = damage*3;
        mob::GiveAttack(CurrentAttack);
    };
}

//Share Attack to main.cpp
void mob::GiveAttack(int& CurrentAttack)
{
    CurrentAttack = attack;
}




//TAKE HIT
//Take damage from other mob
void mob::TakeHit(int attack)
{
    if (rand()%101 > agility)
    {
        Damage(attack);
    }
    else
    {
        this -> attack = 0;
        cout << "You missed" << endl;
    }

}

void mob::Damage(int attack) //Health calculation
{
    //Damage Calculation
    if (attack - armor <= 0)
        this -> attack = 0;
    else
        this -> attack -= armor;

    //Health calculation
    if (health - attack <= 0)
    {
        cout << name << " is dead" << endl;
        this -> health = 0;//Health < 0 -> Death
    }
    else
        this -> health -= attack; //Health Calculation
}






//Public

void mob::Return()
{
    cout << name << " took " << attack << " damage" << endl;
    cout << name << " has " << health << " HP left" << endl;
}


bool mob::Health()
{
    if (health == 0)
        return 0;
    else
        return 1;
}

//Constructor
mob::mob(int health, int damage, int armor, string name, int agility)
{
    this -> health = health;
    this -> damage = damage;
    this -> armor = armor;
    this -> name = name;
    this -> agility = agility;
}

#包括
#包括
#包括
#包括
#包括
#包括“mob.h”
使用名称空间std;
int main()
{
//随机种子
srand(时间(nullptr));
//播放器初始化
//Mobs初始化
移动骨架{rand()%51+50,rand()%21+30,rand()%11,“骨架”,rand()%11+10};
移动僵尸{rand()%101+100,rand()%11+10,rand()%21,“僵尸”,rand()%11};
移动蜘蛛{rand()%21+30,rand()%11+20,rand()%11,“蜘蛛”,rand()%21+50};
暴民爬虫{rand()%16+5,rand()%51+50,0,“爬虫”,rand()%6};
//一群暴徒
媒介暴徒;
暴徒。推回(骷髅);
暴徒。推回(僵尸);
暴徒。推回(蜘蛛);
暴徒。推回(爬行动物);
//搏斗
int CurrentAttack=0,n;
而(1可能某些变量未初始化。请检查代码中是否有任何警告。不管问题是什么-您的变量命名约定很糟糕。您可以将
this->attack
替换为
attack
引用类成员,而在
attack
引用参数的相同范围内。这样做不会有任何问题如果在
if(健康-攻击
if(n>mobs.size()-1){printf(“这个敌人已经死了”)中,感到惊讶;goto-tryAgain;
另外,您不需要这些块,请删除它们-为什么要在已匹配的
案例中检查
n
的值:
语句!!除了删除
goto
之外,不建议全部删除,键入:
默认值:继续;
这将返回到循环的开头。这是什么东西
int&CurrentAttack
,函数可以返回一个
int
,可能某个变量未初始化。请检查代码中是否有任何警告。不管问题是什么,您的变量命名约定很糟糕。您可以在sa中将
this->attack
交替写入引用类成员的
attack
me作用域为
attack
引用一个参数。如果在
if(健康-攻击
if(n>mobs.size()-1){printf(“这个敌人已经死了”)中;goto-tryAgain;
另外,您不需要这些块,请删除它们-为什么要在已匹配的
案例中检查
n
的值:
语句!!除了删除
goto
之外,不建议全部删除,键入:
默认值:继续;
这将返回到循环的开头。这是什么东西
int&CurrentAttack
,函数可以返回
int

#include "mob.h"

using namespace std;

//Attack function
void mob::Attack(int& CurrentAttack) {
    int n;
    cout << "Choose what attack to use:\n1 - Strong Attack;\n2 - Simple Attack;\n3 - Critical Strike;\n";
    cin >> n;
    switch (n) {
        case 1:
            StrongAttack(CurrentAttack);
            break;
        case 2:
            SmallAttack(CurrentAttack);
            break;
        case 3:
            Crit(CurrentAttack);
            break;
        default:
            SmallAttack(CurrentAttack);
            break;
    }
}

//Types of Attack
void mob::StrongAttack(int& CurrentAttack)
{
    this -> attack = damage*2;
    mob::GiveAttack(CurrentAttack);
}
void mob::SmallAttack(int& CurrentAttack)
{
    this -> attack = damage;
    mob::GiveAttack(CurrentAttack);
}
void mob::Crit(int& CurrentAttack)
{
    if (rand() % 2)
    {
        this -> attack = damage*3;
        mob::GiveAttack(CurrentAttack);
    };
}

//Share Attack to main.cpp
void mob::GiveAttack(int& CurrentAttack)
{
    CurrentAttack = attack;
}




//TAKE HIT
//Take damage from other mob
void mob::TakeHit(int attack)
{
    if (rand()%101 > agility)
    {
        Damage(attack);
    }
    else
    {
        this -> attack = 0;
        cout << "You missed" << endl;
    }

}

void mob::Damage(int attack) //Health calculation
{
    //Damage Calculation
    if (attack - armor <= 0)
        this -> attack = 0;
    else
        this -> attack -= armor;

    //Health calculation
    if (health - attack <= 0)
    {
        cout << name << " is dead" << endl;
        this -> health = 0;//Health < 0 -> Death
    }
    else
        this -> health -= attack; //Health Calculation
}






//Public

void mob::Return()
{
    cout << name << " took " << attack << " damage" << endl;
    cout << name << " has " << health << " HP left" << endl;
}


bool mob::Health()
{
    if (health == 0)
        return 0;
    else
        return 1;
}

//Constructor
mob::mob(int health, int damage, int armor, string name, int agility)
{
    this -> health = health;
    this -> damage = damage;
    this -> armor = armor;
    this -> name = name;
    this -> agility = agility;
}