我无法弄清楚为什么这个while循环是无止境的

时间:2013-11-01 01:06:22

标签: c++ function loops while-loop

以下是我目前正在使用的代码:

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

每当我运行它时,它会创建一个没有文本出现在终端中的emdless循环。函数roll2Dice()使用rand()函数模拟两个六面骰子的滚动,并将两个结果相加。任何帮助将不胜感激。

4 个答案:

答案 0 :(得分:2)

你的同时测试,crapsResult != true || crapsResult != false必然是真的,因此不会终止。 crapsResult值为true,这将使表达式的两半中的一半为true,一个为false。 true || falsefalse || true都评估为true

答案 1 :(得分:1)

正如其他人所说,很明显为什么循环永远不会结束。

你使用NULL让我觉得你希望你有一个可以容纳三个值的bool(unset,true和false)。你可以通过使用指向bool的指针以最小的改变来实现这一点,但这非常恶心。枚举是你真正需要的:

enum CrapsResult
{
    unrolled,
    true_result,
    false_result
};

然后相关代码变为*:

CrapsResult crapsResult = unrolled;
int currentGameStorage[100];
int currentRoll = 1;
int point = roll2Dice();
int printingNumber = 0;
currentGameStorage[0] = point;
if(point == 7 || point == 11)
{
    crapsResult = true_result;
}
else if(point == 2 || point == 3 || point == 12)
{
    crapsResult = false_result;
}
else
{
    crapsResult = unrolled;
}

while(crapsResult == unrolled)
{
    currentGameStorage[currentRoll] = roll2Dice();
    if(currentGameStorage[currentRoll] == point)
    {
        crapsResult = true_result;
    }
    else if(currentGameStorage[currentRoll] == 7)
    {
        crapsResult = false_result;
    }
    currentRoll += 1;
}

*我实际上没有编译过这个。

答案 2 :(得分:0)

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

等于

while(crapsResult == false || crapsResult == true)

//while(crapsResult == (false || true)) // Don´t matter

因为boolean只有false或true

它将始终评估 true 并等于此表达式

while(true)

答案 3 :(得分:0)

在C和C ++中,表达式可以根据以下内容解析为true或false:0为false,其他所有内容均为true。 C&#39的宏FALSE和TRUE的正式定义是#define FALSE 0#define TRUE (!(FALSE))

bool类型的变量可以是truefalse。您使用NULL值初始化crapsResult。 NULL是定义为0UL0ULL的宏,其值为false。

bool crapsResult = NULL;

相当于

bool crapsResult = false;

因此,当您的代码执行以下操作时:

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

总是满足其中一个条件,因此循环总是重复。

您需要第二个变量,大概是bool类型,以确定您是否有可接受的答案。

bool crapsResult = false;
bool haveCrapsResult = false;
int currentGameStorage[100];
int currentRoll = 1;
int point = roll2Dice();
int printingNumber = 0;
currentGameStorage[0] = point;
if(point == 7 || point == 11)
{
    crapsResult = true;
    haveCrapsResult = true;
}
else if(point == 2 || point == 3 || point == 12)
{
    crapsResult = false;
    haveCrapsResult = false;
}

while(!haveCrapsResult)
{
    currentGameStorage[currentRoll] = roll2Dice();
    if(currentGameStorage[currentRoll] == point)
    {
        crapsResult = true;
        haveCrapsResult = true;
    }
    else if(currentGameStorage[currentRoll] == 7)
    {
        crapsResult = false;
        haveCrapsResult = true;
    }
    currentRoll += 1;
}
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) is redundant */
    {
        cout << "lose";
    }
    cout << endl;
}
return crapsResult;