尝试获取用户输入并输出一条消息,期望当我输入一个有效名称时它会输出所有这些信息
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 消息,例如:The Type Of Ammo For The sks Is 308 WindchesterThe Type Of Ammo For The sks 7.62x39mmThe sks.357
的弹药类型
如果这样的条件不行。
如果你想要这个,你必须这样写:
if (gun == "izh rifle" || gun == "IZH Rifle" || gun == "izhrifle" || gun == "izh rifle" ||
gun == "sks" ||gun == "SKS" ||gun == "akm" ||gun == "AKM")
基本上正确的比较需要比较两个值,而不是一个。
你不能在 C++ 中编写这样的布尔表达式。布尔表达式的每一面都是布尔值,所以在您的情况下,所有三个 if 始终为真,因为非空字符串在布尔表达式中为真。
请阅读布尔表达式并写下类似的东西
如果(输入== "str1" || 输入== "str2" 等)
#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 消息,例如:The Type Of Ammo For The sks Is 308 WindchesterThe Type Of Ammo For The sks 7.62x39mmThe sks.357
的弹药类型如果这样的条件不行。 如果你想要这个,你必须这样写:
if (gun == "izh rifle" || gun == "IZH Rifle" || gun == "izhrifle" || gun == "izh rifle" ||
gun == "sks" ||gun == "SKS" ||gun == "akm" ||gun == "AKM")
基本上正确的比较需要比较两个值,而不是一个。
你不能在 C++ 中编写这样的布尔表达式。布尔表达式的每一面都是布尔值,所以在您的情况下,所有三个 if 始终为真,因为非空字符串在布尔表达式中为真。 请阅读布尔表达式并写下类似的东西 如果(输入== "str1" || 输入== "str2" 等)