The rule is a simple one. For the body of a loop or conditional, you can omit braces if there's only a single statement:
Code:
if ( condition )
cout<<"booga!\n";
for ( int i = 0; i < 10; i++ )
cout<< i <<'\n';
But, if there is more than one statement, you must use braces or only the first statement will be treated as the body:
Code:
for ( int i = 0; i < 10; i++ )
cout<<"i is: ";
cout<< i <<'\n';
The above loop will print:
Code:
i is: i is: i is: i is: i is: i is: i is: i is: i is: i is: 10
Surrounding the loop body in braces fixes the problem:
Code:
for ( int i = 0; i < 10; i++ ) {
cout<<"i is: ";
cout<< i <<'\n';
}
Thus printing:
Code:
i is: 0
i is: 1
i is: 2
i is: 3
i is: 4
i is: 5
i is: 6
i is: 7
i is: 8
i is: 9
Your problem is that the else keyword expects the body of an if statement immediately before it. With accurate formatting, this is your conditional:
Code:
if (num2 = 0)
cout << num1 << ' ' << operation << ' ';
cout << num2 << ' ' << '=' << ' ' << "ERROR";
cout << "Cannot divide by zero" << endl;
else
cout << num1 << ' ' << operation << ' ';
cout << num2 << ' ' << '=' << ' ' << num1 / num2;
Because there are two calls to cout's << operator between the if body and the else keyword, the else has no if to pair with, and the compiler flags an error. The solution is to use braces:
Code:
if (num2 = 0) {
cout << num1 << ' ' << operation << ' ';
cout << num2 << ' ' << '=' << ' ' << "ERROR";
cout << "Cannot divide by zero" << endl;
}
else {
cout << num1 << ' ' << operation << ' ';
cout << num2 << ' ' << '=' << ' ' << num1 / num2;
}