-1

下面列出了我正在使用的完整代码,它应该模拟掷骰子游戏并向用户打印详细信息,并允许在用户愿意时下注。除了实际的掷骰子游戏外,一切都正常运行。它不是只在没有与 crapResult 关联的真值时循环,而是找到一个实值和一个由单个负数组成的难以理解的字符串。任何帮助,将不胜感激。

int main()
{
    //Declare the user input variables
    int gamesPlayed = 0;
    char inputPrint = ' ';
    char isBetting = ' ';
    int startingBet = 0;

    //Declare the variables used by the program
    int endingBet = 0;
    int currentGame = 0;
    bool crapsResult;
    int gamesWon = 0;
    int gamesLost = 0;
    double percentWon = 0;
    bool detailPrint = false;

    //Prompt the user to input their variables
    cout << "Enter the number of games to be played: ";
    cin >> gamesPlayed;
    while(gamesPlayed < 1)
    {
        cout << "   Error: must be greater than 0" << endl;
        cout << "Enter the number of games to be played: ";
        cin >> gamesPlayed;
        cin.clear();
        cin.ignore();
    }

    cout << "Do you wish to print details (Y/N): ";
    cin >> inputPrint;
    if(inputPrint == 'y' || inputPrint == 'Y')
    {
        detailPrint = true;
    }

    cout << "Do you wish to bet (Y/N): ";
    cin >> isBetting;

    if(isBetting == 'y' || isBetting == 'Y')
    {
        cout << "Enter money to start betting with: ";
        cin >> startingBet;
        while(startingBet < 1)
        {
            cout << "   Error: must be greater than 0" << endl;
            cout << "Enter the number of games to be played: ";
            cin >> gamesPlayed;
            cin.clear();
            cin.ignore();
        }
    }
    //Seed the random number generator
    srand(time(NULL));

    //Set a value for ending bet
    if(startingBet == 0)
    {
        endingBet = 1;
    }
    else
    {
        endingBet = startingBet;
    }
    //Call playcraps to simulate the game for as many games as the user input
    for(currentGame = 1; currentGame <= gamesPlayed && endingBet > 0; currentGame += 1)
    {
        crapsResult = NULL;
        crapsResult = playCraps(currentGame, detailPrint, isBetting, startingBet);
        if(crapsResult == true)
        {
            gamesWon += 1;
            endingBet = betting(endingBet, crapsResult);
        }
        if(crapsResult == false)
        {
            gamesLost += 1;
            endingBet = betting(endingBet, crapsResult);
        }
        if((isBetting == 'Y' || isBetting == 'y') && (detailPrint == true))
        {
            cout << "Money left is $" << endingBet << endl;
        }
    }

    //Calculate the percentage of games won
    percentWon = (double(gamesWon) / double(currentGame-1)) * 100.0;

    //Print the results to the user
    if(isBetting == 'Y' || isBetting == 'y')
    {
        cout << "Money at end of games is $" << endingBet << endl;
    }
    cout << "The number of games played is " << currentGame - 1 << endl;
    cout << "The number of games won is " << gamesWon << endl;
    cout << "The number of games lost is " << gamesLost << endl;
    cout << "The percent of games won is " << fixed << showpoint << setprecision(3) << percentWon << endl;
}

//Simulates the roll of a single die and returns the result
int roll()
{
    int rollResult = 0;
    rollResult = rand() % 6 + 1;
    return rollResult;
}

//Calls roll twice and returns the sum of the two results
int roll2Dice()
{
    //Declare variables for this function
    int rollOne = 0;
    int rollTwo = 0;
    int rollSum = 0;

    //Find rollOne and rollTwo
    rollOne = roll();
    rollTwo = roll();

    //Find rollSum
    rollSum = rollOne + rollTwo;

    return rollSum;
}

bool playCraps(int currentGame, bool detailPrint, char isBetting, int startingBet)
{
    bool crapsResult = NULL;
    int currentGameStorage[100];
    int currentRoll = 1;
    int point = roll2Dice();
    int printingNumber = 0;
    currentGameStorage[0] = point;
    if(point == 7 || point == 11)
    {
        crapsResult = true;
    }
    else if(point == 2 || point == 3 || point == 12)
    {
        crapsResult = false;
    }
    else
    {
        crapsResult = NULL;
    }
    while(crapsResult != true && crapsResult != false)
    {
        currentGameStorage[currentRoll] = roll2Dice();
        if(currentGameStorage[currentRoll] == point)
        {
            crapsResult = true;
        }
        else if(currentGameStorage[currentRoll] == 7)
        {
            crapsResult = false;
        }
        currentRoll += 1;
    }
    if(detailPrint == true)
    {
        cout << "Game " << currentGame << ": ";
        for(printingNumber = 0; printingNumber <= currentRoll; printingNumber += 1)
        {
            cout << currentGameStorage[printingNumber] << " ";
        }
        if(crapsResult == true)
        {
            cout << "win";
        }
        else if(crapsResult == false)
        {
            cout << "lose";
        }
        cout << endl;
    }
    return crapsResult;
}

int betting(int endingBet, bool crapsResult)
{
    if(crapsResult == true)
    {
        endingBet += 1;
    }
    else if(crapsResult == false)
    {
        endingBet -= 1;
    }
    return endingBet;
}
4

2 回答 2

3

只是略读并没有阅读您的所有代码(因此可能还有其他错误),但这行肯定是有问题的:

while(crapsResult != true && crapsResult != false)

逻辑上不可能crapsResult同时为,因此永远不会进入循环。

于 2013-11-01T01:33:14.123 回答
1

我相信 Turix 得到了正确的错误,但我会把重点放在不同的地方:

bool crapsResult = NULL;

您正在尝试将 crapResult 用于三个不同的值(truefalseNULL。但是,NULL通常有一个整数值0,它转换为一个布尔值false,所以你的循环永远不会进入。

然后第二个错误开始发挥作用:currentRoll此时1,您尝试打印currentGameStorage从索引 0 到 1(包括)的内容,currentGameStorage[1]尚未分配。这就是为什么您在输出中得到神秘数字的原因。这是一个普遍的错误:你的代码总是试图打印一个项目太多。在循环头中使用<代替<=来解决这个问题:

for(printingNumber = 0; printingNumber < currentRoll; printingNumber += 1)
于 2013-11-01T01:46:00.237 回答