Note that there are some explanatory texts on larger screens.

plurals
  1. POCurly brackets and the returned value in the main method
    text
    copied!<p>I read this <a href="http://www.stroustrup.com/Programming/Solutions/Ch4/e4-5.cpp" rel="nofollow">code</a> (by Bjarne Stroustrup). I am confused... The <code>main</code> function body is not in the <code>{}</code>, and function doesn't return value (as <code>int</code>). And it works... Why?</p> <pre><code>#include "std_lib_facilities.h" int main() try { cout&lt;&lt; "please enter two floating-point values separated by an operator\n The operator can be + - * or / : "; double val1 = 0; double val2 = 0; char op = 0; while (cin&gt;&gt;val1&gt;&gt;op&gt;&gt;val2) { // read number operation number string oper; double result; switch (op) { case '+': oper = "sum of "; result = val1+val2; break; case '-': oper = "difference between "; result = val1-val2; break; case '*': oper = "product of "; result = val1*val2; break; case '/': oper = "ratio of"; if (val2==0) error("trying to divide by zero"); result = val1/val2; break; //case '%': // oper = "remainder of "; // result = val1%val2; // break; default: error("bad operator"); } cout &lt;&lt; oper &lt;&lt; val1 &lt;&lt; " and " &lt;&lt; val2 &lt;&lt; " is " &lt;&lt; result &lt;&lt; '\n'; cout &lt;&lt; "Try again: "; } } catch (runtime_error e) { // this code is to produce error messages; it will be described in Chapter 5 cout &lt;&lt; e.what() &lt;&lt; '\n'; keep_window_open("~"); // For some Windows(tm) setups } catch (...) { // this code is to produce error messages; it will be described in Chapter 5 cout &lt;&lt; "exiting\n"; keep_window_open("~"); // For some Windows(tm) setups } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload