当我输入一个有效的名字时,试图获得用户输入和cout一条消息,它会让所有人都知道

时间:2017-06-05 07:19:16

标签: c++ stringstream

#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 0.357

2 个答案:

答案 0 :(得分:0)

如果条件不起作用。 如果你想要这个,你必须写成:

 if (gun == "izh rifle" || gun ==  "IZH Rifle" || gun == "izhrifle" || gun ==  "izh rifle" || 
   gun ==  "sks" ||gun ==  "SKS" ||gun ==  "akm" ||gun ==  "AKM")

基本上正确的比较需要比较两个值,而不仅仅是一个。

答案 1 :(得分:0)

你不能在C ++中编写这样的布尔表达式。布尔表达式的每一边都是布尔值,所以在你的情况下,所有三个if都是true,因为非空字符串在布尔表达式中是真的。 请阅读有关布尔表达式的内容并写出类似的内容 如果(输入==&#34; str1&#34; ||输入==&#34; str2&#34;等等)