Why wont the if look for both Peter and John?

#include <iostream>
#include <string>
using namespace std;
int main()
{
string Name, Mood; // obvious string
cout << "Whats your name?" << endl;
cin >> Name;
    if (Name == "Peter"|| "John") {

    cout << "Hello, master! How are we feeling to day?" << endl;

    cin >> Mood;

    }else{

        cout << "Hello, " << Name << "!!!";
         }
return 0;
}

Line 9 is not doing what you think. The compiler would complete the OR before doing the comparison. So that's a complete miss.

You should rewrite that to check each name on it's one or try the old is this string in that string (contains.)
example:

if ("Peter John".find(name) != string::npos) {
//.. found.
} 

Alter to fix or to taste.

Here's roughly how the compiler processes this code

if (Name == "Peter"|| "John")
{
    cout << "Hello, master! How are we feeling to day?" << endl;
    cin >> Mood;
}
else
{
    cout << "Hello, " << Name << "!!!";
}

This

if (Name == "Peter" || "John")

is not this

if ((Name == "Peter") || (Name == "John")) // I'm guessing this is what you wanted

It is actually this

if ((Name == "Peter") || ("John" != NULL))

"John" is stored at some address somewhere, so "John" does not equal NULL, so("John" != NULL) is true, so the above turns into

if ((Name == "Peter") || true)

Anything OR'd with true is true, so the above optimizes to

    if(true)

So going back to the original, it is now this

if (true)
{
    // this executes regardless of what Name stores
    cout << "Hello, master! How are we feeling to day?" << endl;
    cin >> Mood;
}
else
{
    // this never executes regardless of what Name stores.
    cout << "Hello, " << Name << "!!!";
}

Since the else part can never execute, the compiler can optimize it out, so now we have

if (true)
{
    // this executes regardless of what Name stores
    cout << "Hello, master! How are we feeling to day?" << endl;
    cin >> Mood;
}

which is this (why bother with the if statement if it's always true?) ...

    cout << "Hello, master! How are we feeling to day?" << endl;
    cin >> Mood;

One could optimize even further and take the Name variable out since it's completely irrelevant, and you do nothing with Mood either, so heck, let's take that out too. Here's your fully "optimized" program.

#include <iostream>
using namespace std;
int main()
{
    cout << "Hello, master! How are we feeling to day?" << endl;
    return 0;
}

So change the line 9 if statement to if ((Name == "Peter") || (Name == "John")) to accomplish what you were going for.

commented: Thank you, you are the best ;) +0

@Assertnull. I like my one liner since to add names I don't have to add more tests. Just plonk the name I want in the big string and let the find find it.

What if my name's Josh and the master's name is Joshua? I type my name in and voila, I'm allowed to play Thermonuclear War with your code when I was only supposed to be able to play a nice game of chess? You'll feel pretty silly when we hit Defcon 1.

commented: You got a point there :) +15
commented: Game and match. +11
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.