【问题标题】:c++ for loop does not terminatec ++ for循环不会终止
【发布时间】:2017-12-17 11:24:15
【问题描述】:

这可能是一个非常简单的问题,但是当我输入 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 播放,按 s 停止

谁能帮我解决这个问题?我会很感激的

【问题讨论】:

标签: c++ for-loop if-statement


【解决方案1】:

问题出在if (playstop == 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;
    }

}

【讨论】:

  • 对不起,我可能一直不清楚。但是“”只是为了说明问题而不是在代码中。在代码中我只有 s
  • 然后试试这个if (playstop == 's') { 这不是完整的答案,你能把你的变量定义展示给更多的信息吗?
  • 谢谢!显然,这完成了工作!我定义了char playstopchar s
  • 然后将我的答案标记为答案))
猜你喜欢
  • 1970-01-01
  • 2018-10-02
  • 2011-11-02
  • 1970-01-01
  • 2018-12-18
  • 2013-03-25
  • 1970-01-01
  • 1970-01-01
  • 2021-01-19
相关资源
最近更新 更多