尝试获取用户输入和cout消息期望,当我输入一个有效名称时,它将所有这些命名
Trying to get user input and cout a message expect when I enter a valid name it couts all of them
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main() {
string gun = "";
cout << "Enter The Gun You Would Like To Know The Type Of Ammo For: n";
getline(cin, gun);
if (gun == "b95" || "B95" || "windchester" || "wind chester" || "Windchester" || "Wind Chester" || "longhorn" || "Long Horn" || "fal" || "FAL")
{
cout << "The Type Of Ammo For The " << gun << " Is 308 Windchester";
}
if (gun == "izh rifle" || "IZH Rifle" || "izhrifle" || "izh rifle" || "sks" || "SKS" || "akm" || "AKM")
{
cout << "The Type Of Ammo For The " << gun << " 7.62x39mm";
}
if (gun == "Mangnum" || "mangnum" || "Repetor" || "repetor")
{
cout << "The Type Of Ammo For The " << gun << ".357";
}
return 0;
}
例如,当程序运行时,例如,我将输入SKS,它将输出所有COUT消息,例如:SKS的弹药类型为308 Windchester,SKS 7.62x39mm SKS的弹药类型为SKS的类型.357
如果条件不起作用。如果需要的话,必须像以下方式写下:
if (gun == "izh rifle" || gun == "IZH Rifle" || gun == "izhrifle" || gun == "izh rifle" ||
gun == "sks" ||gun == "SKS" ||gun == "akm" ||gun == "AKM")
基本上正确的比较需要比较两个值,而不仅仅是一个。
您不能在C 中写下这样的布尔表达式。布尔表达的每一侧都是布尔值,因此在您的情况下,这三个始终是真实的,因为非空字符串在布尔表达中是一个真实的字符串。请阅读有关布尔表情的信息,并像if(input ==" str1" || input ==" str2"等(
相关文章:
- 文本 RPG - 使用函数检查有效的输入
- 如何检查输入是否有效?
- 如何检查用户的输入是否有效以及我正在寻找的数字?
- 如何在使用 cin 请求 int 时有效地使用户输入万无一失?
- 如何知道文本文件中的输入是否是 C++ 中的有效数字
- 拆分字符串输入并查找字符是否有效
- 菜单选择器不接受有效输入
- 如果不有效,如何重复用户输入电子邮件
- 尝试获取用户输入和cout消息期望,当我输入一个有效名称时,它将所有这些命名
- 检查字符串是否与可能的输入之一匹配的最有效方法是什么?
- 循环try..catch ..确保输入有效或使用控制语句
- 测试用户输入数组中的重复项的最有效方法是什么?
- 如何检查字符串输入是否为有效的双精度
- 如何以有效的方式将输入参数传递给函数
- 验证用户输入是否有效 [从 'char' 到 'char*' 的转换无效]
- 我如何让我的部分代码循环并重复一个问题,直到答案是有效的输入C++
- 为什么即使输入有效,也总是设置 cin.failbit
- 输入有效的输入值后仍有提示
- 从C++输入中收集数据时,是否有更有效的方法可以处理变量
- 如果所有输入都有效,则打印总和,否则在 C++ 中打印"Invalid"