c++ - std::string == operator not working in code
问题描述
So I am making a simple calculator using c++ which inputs a string from the user and takes that input as the operation.
std::cout<<"Enter your operation: ";
std::string operation;
std::cin>>operation;
while(operation != string("+")|| operation != string("-") || operation != string("*"))
{
std::cout<<"Invalid operation! Please enter a valid one: ";
std::cin>>operation;
}
However no matter what I input, I get the error message "Invalid operation! Please enter a valid one: ". Please help me out here, thanks!
解决方案
你几乎拥有它。您只需要在循环条件中使用布尔运算符而 &&
不是或: ||
std::cout<<"Enter your operation: ";
std::string operation;
std::cin>>operation;
while(operation != string("+") && operation != string("-") && operation != string("*"))
{
std::cout<<"Invalid operation! Please enter a valid one: ";
std::cin>>operation;
}
只要operation
is not a+
并且 is not a-
并且 is not a ,您就想继续循环*
。因此,如果它是其中之一,则条件将是false
并且您的循环将结束。
推荐阅读
- mysql - SQL 将数据从一列复制到另一列
- oracle - 在 DocuSign API 返回重定向 URI 时收到证书错误消息
- instagram - 适用于移动设备的 Instagram 基本展示 API
- ms-access-2013 - 在加载时需要表单去记录 =Date()
- mysql - MYSQL GROUP BY 具有特定列的值对
- python - Pip install 无法在 Ubuntu 上安装任何软件包
- c# - 如何在 DataGrid WPF C# 中以编程方式选择和聚焦一行
- node.js - 匿名函数 import/no-anonymous-default-export 的意外默认导出
- elasticsearch - 通过 JHipster 使用 Elasticsearch 进行地理定位
- python - 使用数据集创建不重复的特定列的输入计数字典