当字符串是某个单词时给出输出?
Give an output when a string is a certain word?
#include <iostream>
using namespace std;
int main()
{
string n;
cout << "Enter the name of an automobile: " << endl;
cin >> n;
while( n != "End") {
if( n == ("Tesla" or "Volt" or "Leaf")) {
cout << "Electric" << endl;}
else {
if( n == ("Clarity" or "Mirai")){
cout << "Hydrogen Powered" << endl;}
else {
cout << "Gas Powered" << endl; }}
cout << "Enter the name of an automobile: " << endl;
cin >> n;
}
return 0;
}
它需要说明每辆车是如何供电的。基本上如果我输入"特斯拉",它应该说"电动";"福特"应该出现"汽油动力"。当我输入"结束"时,它就结束了。 我收到此错误 .cpp|16|错误:二进制表达式的操作数无效("std::__1::string"(又名"basic_string,分配器>"(和"布尔"(|
这应该有效!在C++没有or
.使用||
.此外,每次都需要将条件放在if
中,例如if(n=="Tesla"||n=="Volt"||n=="Leaf")
。
#include <iostream>
using namespace std;
int main()
{
string n;
cout << "Enter the name of an automobile: " << endl;
cin >> n;
while( n != "End") {
if( n == "Tesla" || n== "Volt" ||n== "Leaf") {
cout << "Electric" << endl;}
else {
if( n == "Clarity" || n=="Mirai"){
cout << "Hydrogen Powered" << endl;}
else {
cout << "Gas Powered" << endl; }}
cout << "Enter the name of an automobile: " << endl;
cin >> n;
}
return 0;
}
if( n == ("Tesla" or "Volt" or "Leaf"))
需要更改为
if( (n == "Tesla") || (n == "Volt") || (n == "Leaf") )
在执行相同操作的其他地方进行类似的更改
- C++中没有
or
关键字。您需要使用||
- ("特斯拉||"伏特" ||"叶子"(将始终返回真。所以条件将变成 if (n == (true((,这永远不会为真。
相关文章:
- 如何根据单词在文本中出现的概率输出单词
- std::cout输出int时出现编译错误
- 当字符串是某个单词时给出输出?
- 当您在此单词中搜索单词时调整字符数组的大小?
- 尝试重载输出运算符时,我无法遍历对象向量
- 字数统计函数在将单词添加到一组唯一单词时遇到问题
- 按升序输出整数时,将最后一个整数放在新行上
- 输出字符时出现分段错误**
- 尝试在 c++ 中按字母顺序排列单词时出现分段错误
- 如何在函数中输入数字以输出单词?
- PDF 打印机仅在未设置输出文件名时生成 PDF
- 搜索单词时的字母模式逻辑
- 分析我的输出文件时,如何设置IDA加载源文件
- 使用数组和布尔值在输出未知时从输出中删除逗号
- (C++) 打印字符数组字符串的单词时出现问题
- 输出结果时如何将参数化测试合并为一个
- 输出 endl 时的叮当段错误
- 尝试过载输出运算符时出错
- 当没有更多的单词时,如何使用>>停止阅读
- 在 C++ 中将编辑过的内文件保存到输出文件时出现问题