+ 2
Using assignment operater in cout
#include <iostream> using namespace std ; main int; { int a=4,b=5,c; cout<<c=a*b; return 0; } This program doesn't run and it shows an error in which it says operater = is not defiend in cout. Why can't we use = in cout ???
2 Respostas
+ 1
Because output stream object only expected the result of the expression `c = a * b`.
You can follow Fractal's way and have your code then be working. That's because wrapping the expression in parentheses tells the code to evaluate the expression first and insert the evaluation result (which is stored in <c>) into the stream. This is fine.
Alternatively, you can calculate the result first, then output the result as follows:
c = a * b;
cout << c;
+ 1
Thanks lpang
is there any deference between 'c=a*b' and (c=a*b)?