尝试获取用户输入和cout消息期望,当我输入一个有效名称时,它将所有这些命名

Trying to get user input and cout a message expect when I enter a valid name it couts all of them

本文关键字:输入 有效 一个 所有这些 用户 获取 cout 消息 期望      更新时间:2023-10-16
#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"等(