错误C4700:未初始化的局部变量' enemyHealth'用过的

时间:2017-05-02 20:55:29

标签: c++

所以我在弄清楚为什么我收到此错误时遇到了一些麻烦。 我已经编程了大约一个星期,并制作了这个非常简单的游戏。 我无法弄清楚为什么它会冒这个错误。

感谢任何帮助。

#include <iostream>
#include <string>
#include <random>
#include <ctime>
#include <windows.h>

using namespace std;

void battleResults(int enemyHealth);
void battleLoop(int health, int enemyHealth, string selection);
void characterSelection(int health, int enemyHealth, string selection);

int main()
{
// MUH STRINGS
string selection;
string enemy;

// MUH INTS
int health;
int enemyHealth;

cout << "***ULTIMATE COMBAT SIMULATOR***\n";
cout << "***CODED BY OCELOT TOES 2017***\n";

Sleep(1000);

characterSelection(health, enemyHealth, selection);

battleLoop(health, enemyHealth, selection);

battleResults(enemyHealth);

system("PAUSE");
return 0;
}

void characterSelection(int health, int enemyHealth, string selection)
{
string enemy;

cout << "Please choose your race, human or monster!\n";

cin >> selection;

if ((selection == "human") || (selection == "Human"))
{
    cout << "***HEALTH***\nHuman = 1000  Monster = 625\n";
    cout << "You have higher health, but lower attack!\nYou also deal a 
    small amount of damage even when blocking.\n";
    enemy = "monster";
    selection = "human";
    health = 1000;
    enemyHealth = 625;
}
else if ((selection == "monster") || (selection == "Monster"))
{
    cout << "***HEALTH***\nHuman = 1000   Monster = 625\n";
    cout << "You have higher attack, but lower health!\n";
    enemy = "human";
    selection = "monster";
    health = 625;
    enemyHealth = 1000;
}
else
{
    cout << "I'm too lazy to make the game figure out typing errors or just 
    turds, so now you can exit :)\n";
    system("PAUSE");
}

Sleep(1000);

cout << "Okay, so you're a " << selection << ".\n";

Sleep(1000);

cout << "You're fighting a " << enemy << ".\n";

Sleep(1000);

cout << "Let's get this fight started!\n";
}

void battleLoop(int health, int enemyHealth, string selection)
{
// RNG BABY
static mt19937 randomGenerator(time(NULL));
uniform_int_distribution<int> humanAttack(100, 150);
uniform_int_distribution<int> monsterAttack(175, 275);
uniform_real_distribution<float> defenseMultiplier(0.25f, 0.50f);

string uiCombatStatus;
int aCombatStatus;
int damageDealt;
int damageReceived;

while ((health > 0) && (enemyHealth > 0))
{
    cout << "What would you like to do?\nAttack or Defend: " << endl;
    cin >> uiCombatStatus;

    if ((uiCombatStatus == "attack") || (uiCombatStatus == "Attack"))
    {
        aCombatStatus = 1;
    }
    else if ((uiCombatStatus == "defend") || (uiCombatStatus == "Defend"))
    {
        aCombatStatus = 0;
    }

    if ((aCombatStatus == 1) && (selection == "human"))
    {
        damageDealt = humanAttack(randomGenerator);
        damageReceived = monsterAttack(randomGenerator);
        cout << "You chose to attack! You attack for: " << damageDealt << 
        ".\n";
        cout << "The enemy hits you for: " << damageReceived << ".\n";
        enemyHealth = enemyHealth - damageDealt;
        health = health - damageReceived;
        cout << "Enemy's health is at: " << enemyHealth << ".\n";
        cout << "Your health is: " << health << ".\n";
    }
    else if ((aCombatStatus == 0) && (selection == "human"))
    {
        damageDealt = humanAttack(randomGenerator) - 70;
        damageReceived = monsterAttack(randomGenerator) * 
        defenseMultiplier(randomGenerator);
        cout << "You chose to defend! You dealt a small amout of damage: " 
        << damageDealt << ".\n";
        cout << "The enemy hits you for: " << damageReceived << ".\n";
        enemyHealth = enemyHealth - damageDealt;
        health = health - damageReceived;
        cout << "Enemy's health is at: " << enemyHealth << ".\n";
        cout << "Your health is: " << health << ".\n";
    }

    if ((aCombatStatus == 1) && (selection == "monster"))
    {
        damageDealt = monsterAttack(randomGenerator);
        damageReceived = humanAttack(randomGenerator);
        cout << "You chose to attack! You attack for: " << damageDealt << 
        ".\n";
        cout << "The enemy hits you for: " << damageReceived << ".\n";
        enemyHealth = enemyHealth - damageDealt;
        health = health - damageReceived;
        cout << "Enemy's health is at: " << enemyHealth << ".\n";
        cout << "Your health is: " << health << ".\n";
    }
    else if ((aCombatStatus == 0) && (selection == "monster"))
    {
        damageReceived = humanAttack(randomGenerator) * 
        defenseMultiplier(randomGenerator);
        cout << "You chose to defend!\n";
        cout << "The enemy hits you for: " << damageReceived << ".\n";
        health = health - damageReceived;
        cout << "Enemy's health is at: " << enemyHealth << ".\n";
        cout << "Your health is: " << health << ".\n";
    }
}
}

void battleResults(int enemyHealth)
{
if (enemyHealth <= 0)
{
    cout << "***YOU WIN!***" << endl;
}
else
{
    cout << "***YOU LOSE!***" << endl;
}
}

1 个答案:

答案 0 :(得分:2)

  • main中,您创建了一个名为enemyHealth的未初始化变量,并将其传递给函数。
  • 然后,在该函数中,您为结果副本赋予了一个值。然后你从函数返回。
  • 回到main,您再次使用旧的,未初始化的变量。
  • 然后,您将其传递给另一个尝试读取该值的函数 但是,该值不确定,因为与您的信念相反,您从未设置它!

我想也许您打算通过引用来传递,所以它始终只是一个变量。

这实际上是一个编译器警告,它已被某些设置提升为错误。但那很好!

顺便说一句,缩进代码会使我们在不牺牲预期寿命的情况下阅读它。