循环的C 不会终止

c++ for loop does not terminate

本文关键字:终止 循环      更新时间:2023-10-16

这可能是一个简单的问题,但是当我输入s以停止时,我的以下代码不会终止。

for ( roundNr = 1;roundNr <=3; roundNr++) {
    optiongame(roundNr);
    std::cout<<"Do you want to continue with the game? Press s for stop or press p for play n";
    std::cin>> playstop;
    if (playstop == s) {
        break;
    }
}

其中:optiongame是某些功能,具有不同的roundnr,但是这很好。PlayStop是用户的输入,即他是要继续选项游戏还是想停止。为此,他可以按P进行p播放,而S停止

有人可以帮我吗?我会感谢

问题在if (playstop == s) {中,我认为您的代码中有任何名称s的变量,因为这没有任何错误,因为您试图将变量与变量进行比较,但是您需要更改将变量值与" S"进行比较:尝试以下操作:

s = 's';
for ( roundNr = 1;roundNr <=3; roundNr++) {
    optiongame(roundNr);
    std::cout<<"Do you want to continue with the game? Press s for stop or press p for play n";
    std::cin>> playstop;
    if (playstop == s) {
        break;
    }
}