2014-12-04 105 views
-1

在這種情況下,我將如何解決這個問題?我已經看過其他帖子的相同問題,我似乎無法將其應用於此。我之前已經解決了這個問題,但由於某種原因,我不記得我是如何做到的。在C++骰子游戲中未初始化的局部變量

#include <iostream> 
#include <cstdlib> 
#include <Windows.h> 
#include<ctime> 
using namespace std; 
//Tristan Currie 11/20/14 
//Dice Game 
int main() 
{ 
    //variables 
    int die1, die2, dice_total, specialsum; 
    char rerun, reroll; 

    //intro 
    cout << "Welcome to the dice game! I will now explain the rules. \nIf you land on a 7 or 11 on the first role you win. \nIf your sum is 2, 3, or 12 you lose. \nAny other role becomes your special sum. If you roll your special sum before you roll a 7 then you win. \nIf when you roll you get a 7 before your special sum then you lose. "; 
    cout << "\n\nIf you would like to start the game, press enter to do your first roll. "; 
    cin.get(); 
    cout << "\n\nRolling Dice..."; 

    //Suspend program for 2 seconds 
    Sleep(2000); 

    //seed random number generator using the system clock 
    srand(static_cast<unsigned int>(time(0))); 

    //generate a random number between 1 and 6 
    die1 = rand() % 6 + 1; 
    die2 = rand() % 6 + 1; 
    dice_total = die1 + die2; 
    cout << "Done!" << endl << "Dice #1 = " << die1 << "\nDice #2 = " << die2 << "\nDice Total = " << dice_total; 
    cin.get(); 

    if ((dice_total == 7) || (dice_total == 11)) { 
     cout << "Congratulations! You have won the game, press enter to end the program. "; 
     cin.get(); 
     reroll = 'n'; 
    } 
    else if ((dice_total == 2) || (dice_total == 3) || (dice_total == 12)) { 
     cout << "You lost. Press enter to exit. "; 
     cin.get(); 
     reroll = 'n'; 
    } 
    else if ((dice_total != 2) || (dice_total != 3) || (dice_total != 12) || (dice_total != 7) || (dice_total != 11)) { 
     cout << "This is your special sum: " << dice_total << endl; 
     dice_total = specialsum; 
     reroll = 'y'; 
    } 

    while (reroll == 'y') { 
     cout << "\n\nRolling Dice..."; 

     //Suspend program for 2 seconds 
     Sleep(2000); 

     //seed random number generator using the system clock 
     srand(static_cast<unsigned int>(time(0))); 

     //generate a random number between 1 and 6 
     die1 = rand() % 6 + 1; 
     die2 = rand() % 6 + 1; 
     dice_total = die1 + die2; 
     cout << "Done!" << endl << "Dice #1 = " << die1 << "\nDice #2 = " << die2 << "\nDice Total = " << dice_total; 
     cin.get(); 

     if (dice_total == specialsum) { 
      cout << "Congratulations! You have won the game, press enter to end the program. "; 
      cin >> rerun; 
      cin.get(); 
      reroll = 'n'; 
     } 
     else if (dice_total == 7) { 
      cout << "What a shame, you have lost the game, press enter to exit the gane. "; 
      cin >> rerun; 
      cin.get(); 
      reroll = 'n'; 
     } 
     else { 
      reroll = 'y'; 
     } 
    } 
} 
+0

它說什麼局部變量是未初始化的? – CBredlow 2014-12-04 00:44:13

+0

哎呀,我忘了說哪個變量。它說問題出在專精。 – immortalTman 2014-12-04 02:15:26

+0

它看起來像特殊稱呼,但沒有分配給它的值。我會將它作爲答案發布,但我對C++不夠熟悉。 – CBredlow 2014-12-04 02:16:50

回答

0

那麼,我在評論中得到了答案。我混合起來這一行的順序:dice_total = specialsum;

0

int specialsum = 0;

初始化整數變量爲零,或者爲已知值。像這樣定義你的變量,「int specialsum;」 ,它會迷路。 也嘗試specialsum= dice_total;而不是dice_total=specialsum;,因爲「dice_total」會一直等於零。希望這有助於。