+ 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 ???

4th Nov 2019, 6:36 AM
Armina
Armina - avatar
2 Respuestas
+ 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;
4th Nov 2019, 8:21 AM
Ipang
+ 1
Thanks lpang is there any deference between 'c=a*b' and (c=a*b)?
4th Nov 2019, 8:23 AM
Armina
Armina - avatar