Jun 4, 2011 at 9:07pm Jun 4, 2011 at 9:07pm UTC
#include "stdafx.h"
#include <iostream>
using namespace std ;
int main ()
{
int x = 6 ;
cout << x ;
x = 9 ;
cout << x += 1 ;
cin.get () ;
return 0 ;
}
Jun 4, 2011 at 9:30pm Jun 4, 2011 at 9:30pm UTC
<< has higher priority than += so you are not adding to x, but to whatever cout << x returned (which is a reference to cout).
Either wrap the assignment is parenthesis or move it into a separate line.
Jun 4, 2011 at 9:36pm Jun 4, 2011 at 9:36pm UTC
Okay so I must do..this?
x += 1 ;
cout << x ;
I cannot make it add 1 and show me the answer in one statement?
Last edited on Jun 4, 2011 at 9:38pm Jun 4, 2011 at 9:38pm UTC
Jun 4, 2011 at 9:51pm Jun 4, 2011 at 9:51pm UTC
Wrapping the computation in parentheses will change the order of precedence. It's done like this:
Wazzak
Last edited on Jun 4, 2011 at 9:54pm Jun 4, 2011 at 9:54pm UTC