MAYANK SHARMA MAYANK SHARMA - 15 days ago 8
C++ Question

URI 1848 solution

#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!!!!

Answer

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.