c ++骰子游戏中未初始化的局部变量

时间:2014-12-04 00:42:02

标签: c++ initialization

在这种情况下,我该如何解决这个问题?我看过其他帖子有同样的问题,我似乎无法将其应用于此。我之前已经解决了这个问题,但由于某些原因我不记得我是怎么做到的。

#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';
        }
    }
}

2 个答案:

答案 0 :(得分:0)

好吧,我在评论中得到了答案。我混淆了这一行的顺序:dice_total = specialsum;

答案 1 :(得分:0)

int specialsum = 0;

将整数变量初始化为零或已知值。像这样定义你的变量,“int specialsum;” ,它迷路了。 同时尝试specialsum= dice_total;而不是dice_total=specialsum;,因为“dice_total”始终等于零。希望这有帮助。