所以我有一些麻煩搞清楚到底爲什麼我得到這個錯誤。 我已經編程了大約一個星期,並做了這個非常簡單的遊戲。 我只是無法弄清楚它爲什麼會噴出這個錯誤。錯誤C4700:未初始化的局部變量「enemyHealth」使用
任何幫助表示讚賞。
#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;
}
}
而錯誤發生在哪裏? – user3344003
錯誤是什麼? – YosefMac
@YosefMac:看看標題。 –