2014-12-04 3 views
-1

В этом случае, как бы я решил это? Я смотрел на другие сообщения с той же проблемой, и я не могу применить ее к этому. Я решил эту проблему раньше, но по какой-то причине я не могу вспомнить, как я это сделал.Неинициализированная локальная переменная в игре C++ cices

#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

+0

Упс я забыл сказать, какую переменную. В нем говорится, что проблема связана с specialsum. – immortalTman

+0

Похоже, specialsum вызывается, но ему не присвоено никакого значения. Я бы опубликовал это как ответ, но я недостаточно знаком с C++. – CBredlow

ответ

0

Ну, я получил ответ в комментариях. Я перепутал порядок этой линии: dice_total = specialsum;

0

int specialsum = 0;

Инициализация целочисленных переменных к нулю, или к известному значению. Определяя вашу переменную следующим образом: «int specialsum;» , он теряется. Также попробуйте specialsum= dice_total; вместо dice_total=specialsum;, потому что «dice_total» будет равно нулю все время. Надеюсь, это помогло.

Смежные вопросы