平等不是一个bool函数,它返回一个bool。布尔值被用来表示只能是真或假的值。当我们谈论一个值是否相等时,它是(真的),或者它不是(假)。因此,它会更有意义,返回一个bool比int返回int值的函数将在技术上(返回值1,如果参数都是平等的,否则为0),返回一个bool类型更为直观和叶有错误较少的房间。
using namespace std;
cout << "Are you sleeping?" << endl;
cout << "Enter y for yes, n for no:";
char n;
char ch;
cin >> ch;
bool ch(n);
n = true;
if (n)
cout << "You are not sleeping";
else
cout << "You are sleeping";其实你做的不正确。你有点聪明,而不是在C的运营商。
现在你使用操作符!这实际上OR值的所有位,然后赞美它:
很好的教程部分!一些可以提高本节的可读性的评论:
#include <iostream>
using namespace std;
bool IsEqual( int x, int y ) ;
int main()
{
int x ;
int y ;
bool bSecond;
cout<< " First input: " << endl;
cin >> x ;
cout<< " Second input: " << endl;
cin >> x ;
bool bFirst = IsFalse( x, y );
cout<< "They are equal or not equal ?\n -> 1 - Equal ; 0 - Not equal ; " << endl;
cin >> bSecond ;
if( bFirst == bSecond )
{
cout<<" The answer is right! " << endl;
cin.get();
}
else
{
cout<<" The answer is wrong " << endl;
cin.get();
}
cin.get();
}
bool IsEqual( int x, int y)
{
return( x==y );
}1。提到,测试= =如果假和1,如果真的,并讨论了使用这种平等的逻辑“测试”将输出一个0。我花了一些时间去欣赏,为自己。
2。在教程中还没有讨论过逻辑命令(如果不是等),所以有人对编程有可能会发现这个令人困惑的问题。
您还可以提到,如果(1)在一个语句中运行第一个命令,如果(0)运行“其他”语句。有趣的是,如果(5),如果(11)和所有(!= 0)有相同的效果,如(1)。如果语句的上下文中的所有非零值都是真的。
2546

被折叠的 条评论
为什么被折叠?



