我不太明白为什么我的While循环没有捕捉到任何这些情况 [英] I don't exactly understand why my while loop is not catching any of these conditions

查看:39
本文介绍了我不太明白为什么我的While循环没有捕捉到任何这些情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试反向输出输入字符串,当用户输入";Done";、";Done";或";d";时,它将停止。这样,While循环不会捕获这些条件中的任何一种来停止循环。

#include`<iostream>
using namespace std;

int main() {

   string userInput;
   int i;
   char output;
   getline(cin, userInput);

    
    
      while (userInput != "done" || userInput != "Done" || userInput != "d") {
        for (i = userInput.size() - 1; i >= 0; --i) {
            output = userInput.at(i);
            cout << output;
        }
        cout << endl;
        getline(cin, userInput);
    } 
   

   return 0;
}

推荐答案

替换

while (userInput != "done" || userInput != "Done" || userInput != "d")

while ( ! (userInput == "done" || userInput == "Done" || userInput == "d") )

这篇关于我不太明白为什么我的While循环没有捕捉到任何这些情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆