0

For the hacker edition of pset1 I'm working on credit.c. So far, I'm progressing nicely. However, I decided using a Ternary Operator assigning a value to the string variable ccType using the code below:

        ccType =    (firstDigits > 39) && 
                    (firstDigits < 50) ? "VISA" : 
                        (firstDigits => 51) && 
                        (firstDigits <= 55) ? "MASTERCARD" : "INVALID";

make spits out the following error:

credit.c:50:43: error: expected expression
                        (firstDigits => 51) && 
                                      ^

It looks like he doesn't like the "less than or equal to" sign inside the nested ternary operator. I guess I could rewrite it all as an if..else construction, but I'm very curious why I get this error. I've already experimented with putting braces here and there, but no result. Also, I notice the error doesn't come up in the earlier conditions, where "==" or just "<" or ">" is used. Is a Ternary operator limited to those three, and is the part "or equal to" causing the problem here? If yes, does anyone know why?

1 Answer 1

2

Interesting use of ternary operator. But your problem has nothing to do with it.

=> is an invalid operator. It should be >=. It's usually the simple ones that get us. ;-)

If this answers your question, please accept this answer to remove the question from the unanswered question pool. Let's keep up on forum housekeeping. ;-)

2
  • Thanks a lot for the answer Cliff! As a beginning "programmer", I'm curious about your comment on my use of the ternary operator. I know it's just another way of an if..else statement, but I'm trying different things to get acquainted with them. If you can explain why my use here is "interesting" as you call it, it would be very helpful! Commented Aug 2, 2015 at 18:10
  • It's interesting because I've never seen anyone use the concept of nested ternary operators - not in lessons, classes or in practice. While it is perfectly valid and creative here, when you nest something this complex, it gets easy to lose track of the logic. So kudos to you for finding a good place to do this! But that's just my opinion. ;-)
    – Cliff B
    Commented Aug 2, 2015 at 18:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .