无法在布尔条件c ++的循环中退出

0 c++ loops boolean exit while-loop

嗨这是我的第一篇文章.如果我不遵守某些规则或惯例,我会道歉.如果是这种情况,请告诉我.

我有一个游戏在一个循环中运行,直到任一玩家达到得分限制,此时另一个玩家有最后一次(迭代)机会击败第一个玩家得分.但是,在达到分数限制后,循环继续运行并且永远不会检查获胜者.

#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>
using namespace std;

int roll();
int playTurn(int);

int main(){

const int LIMIT = 5;
int whoseTurn = 1;
int pnts1 = 0;
int pnts2 = 0;
bool suddenDeath = false; //True when score limit is reached



while(!suddenDeath){

    if(pnts1 >= LIMIT || pnts2 >= LIMIT){                       //Limit was reached by previous player.
        suddenDeath == true;                                    //Next player has 1 turn to win

    }   

    if(whoseTurn == 1){
        pnts1 += playTurn(whoseTurn);                           //Play turn and tally points
        whoseTurn = 2;                                          //Swith player for next iteration
    }
    else if(whoseTurn == 2){
        pnts2 += playTurn(whoseTurn);
        whoseTurn = 1;
    }

    cout << "-------------------------------------" << endl     //Display score
         << "Player 1 has " << pnts1 << " points" << endl
         << "Player 2 has " << pnts2 << " points" << endl
         << "-------------------------------------" << endl << endl;

};

if(pnts1 > pnts2)
    cout << "Congratulations Player 1! You won with a score of: " << pnts1 << " - " << pnts2;
else if(pnts2 > pnts1)
    cout << "Congratulations Player 2! You won with a score of: " << pnts2 << " - " << pnts1;
else if(pnts1 == pnts2)
    cout << "A tie! What are the chances?";

return 0;

}
Run Code Online (Sandbox Code Playgroud)

pax*_*blo 5

suddenDeath == true;
//          ^^
Run Code Online (Sandbox Code Playgroud)

是一个表达意思是"比较那两个值",然后扔掉.C语句42;同样有效,同样无用(a).

您想要分配值,因此您将使用:

suddenDeath = true;
//          ^
Run Code Online (Sandbox Code Playgroud)

它实际上是人们分配而不是比较的更常见问题的另一if (a = 0).


(a)如果你想知道为什么心智正常的人会允许这种语言成为一种语言,它实际上允许使用最少代码的一些强大的结构.

而且,你最有可能见过它.声明i++;就是这样的野兽.这给人一种表达i与(你扔掉这里)副作用i是继递增.