|
From: Per W. <pw...@ia...> - 2003-09-23 21:20:22
|
Almost correct. If I understand the original goal, I'd recommend:
if (letter2 != 'u' && letter2 != 'v'
&& letter2 != 's' && letter2 != 't'
&& letter2 != 'a') {
cout << "Only u,v,s,t,a are allowed" << endl;
exit();
}
Using || isn't so good since an expression:
if (var != value1 || var != value2)
will degenerate to the constant expression:
if (1)
when value1 != value2.
Note that
if (letter2 != 'u' && letter2 != 'v' && ...)
can be rewritten as
if (!(letter2 == 'u' || letter2 == 'v' || letter2 == ...))
which is a simple rewrite of a logical expression.
Whenver changing between a && expression and a || expression - either
way is done by:
1: Inverting total result:
if (xxx) interchanged with if (!(xxx))
2: Inverting every sub-statement in the expression
== interchanged with !=
< interchanged with >=
etc.
About the original mail:
if (letter2 != u' || 'v' || 's' || 't' || 'a')
is the same as:
if (letter2 != 'u' || 'v' != 0 || 's' != 0 || 't' != 0 || 'a' != 0)
which is a constant expression that is always true:
if (1)
since any character except '\0' are different from 0.
/Per W
On Tue, 23 Sep 2003, Geoff Baker wrote:
> You have to declare each side of the '||' as a seperate check.
>
> ie:
>
> if ((letter2 != 'u') || (letter2 != 'v') || (letter2 != 's') || (letter2 !=
> 't') || (letter2 != 'a')) {
> cout<<"An error has occured, check you typed in only u, v, s, t or
> a."<<endl;
> exit();
> }
>
> I do not know for sure if the brackets inside the '||' are required but this
> should fix your problem
>
> -geoff
>
> -----Original Message-----
> From: - [mailto:ali...@nt...]
> Sent: September 23, 2003 2:00 PM
> To: dev...@li...
> Subject: [Dev-C++] IF [faked-from][sls]
>
>
> problem with if statement:
> if (letter2 != 'u' || 'v' || 's' || 't' || 'a') {
> cout<<"An error has occured, check you typed in only u, v, s, t or
> a."<<endl;
> exit();
> }
>
> Compiles ok but after checking i've realised it believes the values are part
> of an earlier enum declaration:
>
> enum value{u,v,s,t,a};
> value letter;
>
> How do i tell the compiler i want charactors, not enum values?
>
>
|