0

I am trying to use a find_if Boolean function to return true if:

  1. Z is =<10;
  2. name="john" (all lower case)

My code:

/* predicate for find_if */
bool exam_pred(const exam_struct &a)
{
  if ((a.Z=<10)&&(a.name="john")) 
  {
     return true;
  }   
}

exam_struct{
  int x,y;
  double Z;
  string name;
};

It doesn't compile when I set a.name="john". So my question is how do implement the a.name="john"; into my boolean?

Niels Keurentjes
  • 41,402
  • 9
  • 98
  • 136
  • 1
    Warning. Not all control paths return a value... – Armen Tsirunyan May 12 '13 at 22:48
  • 1
    When you say *"it doesn't compile"* to a fellow programmer it's similar to going to a doctor and saying *"something hurts"*. Be specific, share the errors you're getting. – Niels Keurentjes May 12 '13 at 22:48
  • @ArmenTsirunyan while a valid comment, this will only give a warning or notice depending on compiler settings, and fall back to default return value of `0`, equivalent to `false`. It will thus not break the code. – Niels Keurentjes May 12 '13 at 22:49
  • 2
    @Niels You're very mistaken. Failing to return a value from a function with a non-void return type (other than main) is undefined behavior. – Jim Balter May 12 '13 at 23:40
  • Funny, I had to look hard to find a [standards-quoting source](http://stackoverflow.com/a/3402194/1729885), but you're right. I'm pretty sure most compilers default it to 0 (possibly only in debug builds), but indeed TS should explicitly `return false` at the end of the `exam_pred` function. +1 for you anyway hehe. – Niels Keurentjes May 12 '13 at 23:45
  • @Niels - most compilers just leave the register where they return values alone, which gives you nonsense. – Pete Becker May 13 '13 at 00:18
  • Makes sense I suppose, it could actually be used for performance optimization in rare cases to eliminate the overhead of setting a return value for certain codepaths. Never thought of that. – Niels Keurentjes May 13 '13 at 00:20
  • @Niels " I'm pretty sure most compilers default it to 0" -- I'm absolutely certain you have no idea what you're talking about. I sure hope you don't write any code I ever use. "possibly only in debug builds" -- It's hard to imagine anything **worse** that a compiler could do. – Jim Balter May 13 '13 at 00:24
  • @JimBalter No need to get personal or offensive, rather childish over such a simple remark. I haven't professionally written serious C++ in about 8 years now, so pardon me for getting a bit rusty about the tiny details, but since my job before that was lead C++ developer in a car manufacturing plant, and that code is still in 24/7 use today, I'm pretty sure your life still depends on my code nearly every day you get out on the road. I made a mistake, you pointed it out, I admitted my error and thanked you, let it rest. – Niels Keurentjes May 13 '13 at 00:37
  • You shouldn't comment about things that you aren't competent to discuss, and you certainly shouldn't be correcting your betters like Armen. It isn't just the incompetence, it's that what you wrote is so dangerous. And now one more reason that I'm glad I ride a bicycle. – Jim Balter May 13 '13 at 00:41

2 Answers2

3

you should indeed use the == operator.

I was wrong suggesting strcmp before, since you are using strings.

code:

struct exam_struct {
    int x, y;
    double Z;
    string name;
};

/* predicate for find_if */
bool exam_pred(const exam_struct& a)
{
    return a.Z <= 10 && a.name=="john";
}

note that in your original code you do not return false when the check false.

Elazar
  • 20,415
  • 4
  • 46
  • 67
  • 1
    strcmp doesn't work on `std::string` (unless you use `a.name.c_str()`, which is "crossing the river to fetch water" as the Swedish saying goes. – Mats Petersson May 12 '13 at 22:46
  • @MatsPetersson already fixed that. I did not read the question carefully. – Elazar May 12 '13 at 22:47
  • @0x499602D2 do you want to explain? – Elazar May 12 '13 at 22:48
  • @Elazar = is assignment, you probably mean `==` – taocp May 12 '13 at 22:49
  • Wow the == operator fixed it. I've been spending all day on my c++ final trying to finish this and couldn't see that error. Thank you all – Donald Waltman May 12 '13 at 23:36
  • @DonaldWaltman And yet the error from the compiler told you about it. Did you see the "=<" instead of "<=", or the undefined behavior when your test fails and you fall through the bottom of the function? – Jim Balter May 13 '13 at 00:09
2

= is the assignment operator. Use == for equality comparisons. And the smaller-or-equals operator is <=, not =<.

Niels Keurentjes
  • 41,402
  • 9
  • 98
  • 136
  • not in the case of C strings, where it is an *identity* comparison. – Elazar May 12 '13 at 22:42
  • 1
    Since the struct is passed by const reference this is C++ (C doesn't have references), and it's thus safe to assume the string is an `std::string`. – Niels Keurentjes May 12 '13 at 22:43
  • Yeah well TS should've tagged that, I added the `C++` tag myself after close inspection hehe. – Niels Keurentjes May 12 '13 at 22:46
  • "I thought it is C string for some reason" -- There's no such thing as a C string. – Jim Balter May 13 '13 at 00:02
  • @JimBalter probably referred to `char*`, as such it would indeed be an identity comparison in a way. – Niels Keurentjes May 13 '13 at 00:09
  • The program contains `string`, not `char*`. That's obviously not C, with or without a reference. – Jim Balter May 13 '13 at 00:23
  • Could've been an externally defined struct, but still - I saw it was C++ right away, no need to preach to the choir ;) – Niels Keurentjes May 13 '13 at 00:24
  • "Could've been an externally defined struct" -- In C that would be neither identity comparison nor content comparison, it would be illegal. "I saw it was C++ right away" -- irrelevant, as I wasn't even talking to you, I was talking Elazar. "no need to preach" -- That's not what I'm doing ... I'm explaining some basics to some people who don't seem to know much about them. – Jim Balter May 13 '13 at 00:32