Search code examples
c++stringstream

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;
}

When the program is run for example i would enter sks and it would output all of the cout messages for example: The Type Of Ammo For The sks Is 308 WindchesterThe Type Of Ammo For The sks 7.62x39mmThe Type Of Ammo For The sks.357


Solution

  • If conditions does not work this way. If you want this, you have to write it like:

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

    Basically proper comparison requires two values to be compared, not just one.