我只是在用C ++做一些实验,但是我不知道为什么两个if语句都返回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;
}
我对C ++和stackoverflow还是很陌生,所以很抱歉这是一个愚蠢或经常被问到的问题,但是我真的不知道了。请帮忙!
lang == "Deutsch" || "deutsch"
是错的
lang == "Deutsch" || lang == "deutsch"
是正确的
仅“ deutsch”返回字符串在内存中的地址。它总是不等于零。这是真的。
a == "hello" || "bob"
方法
(a == "hello") || "bob"
不论a == "hello"
结果是(真还是假),都false || "bob"
变为false || pointer to "bob"
。所有非空指针都是true
,因此false || true
是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;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句