-3

I'm just experimenting a bit with C++ but I can't figure out why both if-statements return true:

#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
    cout << "Language?" << endl;
    string lang;
    cin >> lang;
    if(lang == "Deutsch" || "deutsch")
    {
        cout << "Hallo Welt!";
    }
    else
    {
        return false;
    }
    if(lang == "English" || "english")
    {
        cout << "Hello World!";
    }
    else
    {
        return false;
    }
    return 0;
}

I'm pretty new to C++ and stackoverflow so I'm sorry if that's an stupid or frequently asked question but I really don't know any further. Please help!

hapstyx
  • 37
  • 4
  • Which learning resource taught you to write if statements like this? – Lightness Races in Orbit Aug 16 '15 at 14:32
  • @LightnessRacesinOrbit see the third word of the question – M.M Aug 16 '15 at 14:36
  • 1
    @MattMcNabb: What about it? Are you suggesting that "experimentation" removes the need for prior research here on Stack Overflow? That problems reached only by "guesswork programming" are worthy of anyone's time? Because if so I could not disagree more. – Lightness Races in Orbit Aug 16 '15 at 14:37
  • 2
    No, I'm suggesting that OP was not learning from a resource. If this isn't worth your time just move onto another question. – M.M Aug 16 '15 at 15:01

7 Answers7

4
 lang == "Deutsch" || "deutsch"

is wrong

lang == "Deutsch" || lang == "deutsch"

is right

"deutsch" alone returns the address of the string in memory. which is always not equal to zero. which means true.

a == "hello" || "bob"

means

(a == "hello") || "bob"

regardless of what a == "hello" results in (true or false), false || "bob" becomes false || pointer to "bob". All non-null pointers are true, so this is false || true which is true.

#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
    cout << "Language?" << endl;
    string lang;
    cin >> lang;
    if(lang == "Deutsch" || lang == "deutsch")
    {
        cout << "Hallo Welt!";
    }
    else
    {
        return false;
    }
    if(lang == "English" || lang == "english")
    {
        cout << "Hello World!";
    }
    else
    {
        return false;
    }
    return 0;
}
Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
hasan
  • 23,815
  • 10
  • 63
  • 101
3

The expression lang == "Deutsch" || "deutsch" is actually equivalent to (lang == "Deutsch") || ("deutsch"). The second part of the expression is a const char* with a non-zero value, which means it will evaluate to true. The same applies to your second if statement.

You meant to write lang == "Deutsch" || lang == "deutsch".

user123
  • 8,970
  • 2
  • 31
  • 52
  • 1
    IOW the `==` and `||` are independent binary infix operators, not part of a combined grammar construct. The kind of thing OP is trying to do is a set-inclusion test, which some languages can do with a different spelling - e.g. `2 in [1,2,3]` gives `True` in Python. –  Aug 16 '15 at 13:57
1

The sentence:

if(lang == "Deutsch" || "deutsch")

is interpreted by the compiler as this:

"IF lang is equal to "Deutsch" OR the memory address of the string "deutsch" is not equal to 0 THEN...

Remember that in C/C++ any expression that is not zero, is considered to be TRUE. The expression "deutsch" is a string constant, which as an expression, returns its starting address, which is likely not 0.

mcleod_ideafix
  • 11,128
  • 2
  • 24
  • 32
1

The condition in the if statement

if(lang == "Deutsch" || "deutsch")

is equivalent to the following condition

if( ( lang == "Deutsch" ) ||(  "deutsch" ) )

Independing on whether the left subexpression of the operatir || will yield true of false the right subexpression is always yields true because string literals used in expressions are converted to pointers to their first characters. And because the address of a string literal is not equal to 0 then the subexpression is converted to boolean value true.

It is obvious that you mean the following condition

if(lang == "Deutsch" || lang == "deutsch")

According to the C++ Standard (4.12 Boolean conversions)

1 A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true.

And (5 Expressions)

9 Whenever a glvalue expression appears as an operand of an operator that expects a prvalue for that operand, the lvalue-to-rvalue (4.1), array-to- pointer (4.2), or function-to-pointer (4.3) standard conversions are applied to convert the expression to a prvalue.

Take into account that string literals in C++ have types of constant character arrays as it is said in the quote they are converted to pointers in expressions.

Also you need to include header <string> and should exclude header "windows.h" because neither declaration from the header is used in the program.

Your program then it could be written the following way

#include <iostream>
#include <string>

using namespace std;

int main()
{
    cout << "Language?" << endl;

    string lang;
    cin >> lang;

    if ( lang == "Deutsch" || lang == "deutsch" )
    {
        cout << "Hallo Welt!" << endl;
    }
    else if ( lang == "English" || lang == "english" )
    {
        cout << "Hello World!" << endl;
    }

    return 0;
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

The parts on either side of the || are evaluated as separate boolean expressions. First, your program checks whether lang == "Deutsch". If that's false, it checks the next part, "deutsch". Because this is a non-zero value, it evaluates as true.

Most other variables can be evaluated as boolean in specific (sometimes weird) ways. For an int, 0 is false, and anything else is true. For a string, the null or empty string is false, and anything else is true.

Your final statement should be if(lang == "Deutsch" || lang == "deutsch").

camdroid
  • 524
  • 7
  • 24
-1

if (lang == "Deutsch" || "deutsch") doesn't test what you expect.

it is equivalent to

if (lang == "Deutsch") {
    // Do stuff
} else if ("deutsch") {
    // Do same stuff
}

You wanted

if (lang == "Deutsch" || lang == "deutsch")

"deutsch" decays to a non null pointer so is always true.

Jarod42
  • 203,559
  • 14
  • 181
  • 302
-1

This code

#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
    cout << "Language?" << endl;
    string lang;
    cin >> lang;
    if(lang == "Deutsch" || "deutsch")
    {
        cout << "Hallo Welt!";
    }
    else
    {
        return false;
    }
    if(lang == "English" || "english")
    {
        cout << "Hello World!";
    }
    else
    {
        return false;
    }
    return 0;
}

is the same as

#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
    cout << "Language?" << endl;
    string lang;
    cin >> lang;
    cout << "Hallo Welt!" ;
    cout << "Hello World!";
    return 0;
 }
Ed Heal
  • 59,252
  • 17
  • 87
  • 127