Reputation: 35
include<stdio.h>
main()
{
char m;
int a,b,n=0;
scanf("%c%d%d",&m,&a,&b);
m=='A' || m=='B' || m=='C' ? n=(3*a)+(5*b) : n=(5*a)+(3*b);
printf("%d\n",n);
}
Upvotes: 0
Views: 229
Reputation: 92241
There is no need to use a complicated statement that confuses everyone, including the compiler. This is just as effective, and a lot easier to read:
if (m=='A' || m=='B' || m=='C')
n=(3*a)+(5*b);
else
n=(5*a)+(3*b);
Upvotes: 0
Reputation: 310950
Use instead
m=='A' || m=='B' || m=='C' ? n=(3*a)+(5*b) : ( n=(5*a)+(3*b));
Otherwise the statement looks like
( m=='A' || m=='B' || m=='C' ? n=(3*a)+(5*b) : n)=(5*a)+(3*b);
Or you could write
n = m=='A' || m=='B' || m=='C' ? (3*a)+(5*b) : (5*a)+(3*b);
The conditional operator in C is defined the following way
conditional-expression:
logical-OR-expression
logical-OR-expression ? expression : conditional-expression
As the assignment operator has lower priority then the compiler issues an error because the assignment is excluded from the conditional operator for the third operand
The used by you expression would be valid in C++ because in C++ the operator is defined differently
conditional-expression:
logical-or-expression
logical-or-expression ? expression : assignment-expression
^^^^^^^^^^^^^^^^^^^^^
Upvotes: 2