2017-05-02 75 views
-1

所以我有一些麻煩搞清楚到底爲什麼我得到這個錯誤。 我已經編程了大約一個星期,並做了這個非常簡單的遊戲。 我只是無法弄清楚它爲什麼會噴出這個錯誤。錯誤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; 
} 
} 
+0

而錯誤發生在哪裏? – user3344003

+0

錯誤是什麼? – YosefMac

+0

@YosefMac:看看標題。 –

回答

2
  • main,創建了一個名爲enemyHealth一個未初始化的變量,它傳遞給函數。
  • 然後,在功能,你給所產生的副本的值。然後你從函數返回。
  • 回到main,你再使用舊的,未初始化的變量。
  • 然後,您將它傳遞給另一個函數,該函數嘗試讀取該值。
    然而,該值是不確定,因爲違背了你的信念,你永遠不會把它!

我想也許你打算通過引用來代替,所以它只是一個變量。

這實際上是一個編譯器警告通過某些設置已被提升爲錯誤。但那很好!

順便說一句,縮進你的代碼將讓這個我們可以在不犧牲壽命閱讀。

+0

它實際上是在我的編譯器縮進,但我仍然在學習的繩索stackoverflow的格式化,我的壞! – OcelotToes

+0

@OcelotToes:您將看到一個預覽窗格,您必須滾動瀏覽才能進入「提交」按鈕... –

+0

++用於預期壽命。我已經在45最大。 –