-6
#include <bits/stdc++.h>

#define endl '\n'

using namespace std;

int main() {

  ios_base::sync_with_stdio(false);cin.tie(NULL);

  string line;

  int acc = 0;

  while (cin >> line) {

    if (line == "caw") {

      cin >> line;

      cout << acc << endl;

      acc = 0;

    } else {

      int val = 0;

      for (int i = 0; i < 3; ++i) {

        val <<= 1;

        val += line[i] == '*';

      }
      acc += val;

    }

  }

  return 0;
}

I came across this solution

can anyone please explain what does this val += line[i] == " * "; means?

Please help!!!!

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 1
    `#define endl '\n'`, `using namespace std;` o.O??? – Simon Kraemer Nov 22 '16 at 14:52
  • 3
    First of all, don't blindly copy-paste or otherwise use sources you find on the Internet, not without actually knowing what they do. Secondly, it's a bad program and the inclusion of `` is a clear indicator of it (see [Why should I not #include ?](http://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h) for more information). Thirdly, the program is *really* bad because it uses a macro `endl` which just happens to be the same as `std::endl` that prints a newline and flushes the output. – Some programmer dude Nov 22 '16 at 14:52
  • 1
    @Someprogrammerdude: Technically the program behaviour is *undefined* because of this redefinition. – Bathsheba Nov 22 '16 at 14:53
  • Sure sir i wont blindly copy-paste code from next time. – MAYANK SHARMA Nov 22 '16 at 15:55

1 Answers1

1

val += line[i] == " * "; is evaluated as val += (line[i] == " * "); due to operator precedence.

val will be incremented by 1 (true converts to an integral value of 1) if, and only if, line[i] compares true with " * ", else it will stay the same.

Finally #define endl '\n' is very naughty. Don't attempt to change symbols in the C++ standard library.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483