+ 1

Please, tell me why it doesn't want to work

How can I fix this? https://code.sololearn.com/cG5p18pqTX95/?ref=app

27th Sep 2019, 2:00 PM
Rainbow Pie
4 odpowiedzi
+ 3
Hello Rainbow Pie , I'm not good with maths, and have no idea what that formula about. But I.managed to get the code to run by wrapping the expressions for these lines: cout << "f = "<< (f+=f) << endl; cout << "f = "<< (f*=f) << endl; cout << "f = "<< (f%=f) << endl; cout << "f = "<< (f/=f) << endl; The +=, *=, %= and /= operation apparently need to be wrapped in parentheses. I think it's about statement vs expression or something. But I can't explain, don't know enough about it : )
27th Sep 2019, 2:45 PM
Ipang
+ 4
Ipang Hi, assignments in C++ are expressions instead of statements, they all return a reference of their left hand operand. Operator precedence defines the order of which operations are grouped, much like PEMDAS/BODMAS in math. That means something like a=b+=c and if ((buf = f()) != nullptr) is valid. Here is a list of operators and their precedence https://en.cppreference.com/w/cpp/language/operator_precedence
27th Sep 2019, 3:26 PM
jtrh
jtrh - avatar
+ 2
The bitwise left shift operator has higher precedence than all the compound assignment operators, adding parentheses around the compound assignment may be what you want. https://en.cppreference.com/w/cpp/language/operator_precedence
27th Sep 2019, 2:43 PM
jtrh
jtrh - avatar
0
jtrh Thanks, I saw that link on your initial response too : )
27th Sep 2019, 3:40 PM
Ipang