Reputation:
Following is part of a previously asked code which I'm trying to optimize. This is not part of any app, just trying to make it better in terms of user context. How can make the if
clause work Line 4
with minimal code, so that if user inputs any integer the test succeeds(I want to do it with if
only). You know I'm trying to make out of long if
clause.
int n;
cin >>n;
if(n == (0 || 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9))
// if n is integer
cout<<"succeeded"; //Line 4
else
cout<<"failed";
Upvotes: 1
Views: 59
Reputation: 1
Just declare n as an integer variable. That way the input either won't be read at all or just a 0 if the user enters anything else.
Upvotes: -1
Reputation: 65600
n == (0 || 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9)
This doesn't mean what you think it means. That big logical or
chain will all just collapse to true
, so you're really checking if n == 1
.
You probably just want this:
n >= 0 && n < 10
Upvotes: 6