Code
#include<iostream>
int main()
{
int x;
std::cout<<(std::cin>>x)<<"
";
std::cin.clear();
std::cin.ignore();
if(std::cin>>x)
{
std::cout<<"inside the if-block
";
}
}
Output-1
2 // input-1
0x486650
3 // input-2
inside the if-block
for input 2
cin>>x
succeeds but gives 0x486650
for input 3
if-block executed means it returns 1
.
Output-2
a // input-1
0
b // input-2
for input a
cin>>x
fails and gives 0
as expected.
for input b
cin>>x
if-block not executed means it returns 0
So why std::cout<<(std::cin>>x)
returns something like 0x486650
when succeeds ?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…