Reem
Reem

Reputation: 11

Using multiple != and || in if-statement

I am still new in C++ and I am trying to make my code print "Invalid customer type" when the input(customer) received does not equal "r", "R", "b", or "B". Then it prompts the user again to enter a letter. With the code I've written, "Invalid customer type." is still printed out even when the customer == one of the letters "r", "R", "b", or "B". I have tried making the customer a char instead of string but that didn't change much.

#include <iostream>
#include <string>
using namespace std;

int main ()
{
    string customer;
    do
    {
        cout << "Pleas Enter the Customer Type (R for Regular, B for Business): " << endl;
        cin >> customer;
        if ((customer != "r") || (customer != "R") || (customer != "b") || (customer != "B"))
        {
            cout << "Invalid customer type." << endl;
        }

    } while ((customer != "r") || (customer != "R") || (customer != "b") || (customer != "B"));
    return 0;
}

Upvotes: 1

Views: 89

Answers (2)

Muhammad Muaaz
Muhammad Muaaz

Reputation: 304

If the customer enters r,R,b, orB, then it is an input for the valid customer. That means that the condition for a valid customer will be:

((customer == "r") || (customer == "R") || (customer == "b") || (customer == "B"))

To check the invalidity of the customer, you will have to take the logical not of the above condition which will be:

!((customer == "r") || (customer == "R") || (customer == "b") || (customer == "B"))

This will work. However, you can apply De Morgan's Law to this expression which will result:

((customer != "r") && (customer != "R") && (customer != "b") && (customer != "B"))

Upvotes: 2

Paul Rooney
Paul Rooney

Reputation: 21609

Use && instead of ||

#include <iostream>
#include <string>
using namespace std;

int main ()
{
    string customer;
    do
    {
        cout << "Pleas Enter the Customer Type (R for Regular, B for Business): " << endl;
        cin >> customer;
        if ((customer != "r") && (customer != "R") && (customer != "b") && (customer != "B"))
        {
            cout << "Invalid customer type." << endl;
        }

    } while ((customer != "r") && (customer != "R") && (customer != "b") && (customer != "B"));
    return 0;
}

If we take just the first two conditions, for the sake of simplicity

(customer != "r") || (customer != "R")

One of these conditions will always be true and when using logical OR, if any subcondition evaluates to true, the whole condition evaluates to true.

We need to use logical AND so that if any subcondition is false the whole condition evaluates to false.

This is a bit confusing as you would use || if your conditions were testing for equality rather than inequality.

e.g.

if ((customer == "r") || (customer == "R") || (customer == "b") || (customer == "B"))

You could also refactor your code to be a bit more DRY

int main ()
{
    string customer;
    while(true)
    {
        cout << "Pleas Enter the Customer Type (R for Regular, B for Business): " << endl;
        cin >> customer;
        if ((customer == "r") || (customer == "R") || (customer == "b") || (customer == "B"))
        {
            break;
        }

        cout << "Invalid customer type." << endl;
    } 
}

Upvotes: 3

Related Questions