Using C++ as a Calculator

When you fire up UnderC, you are presented with the command prompt ;>. Now type 2+3;—note the semicolon—and press Enter :

;> 2+3;
(int) 5

UnderC indicates input in black, output in green, and any errors in red; in this book we use bold for input and nonbold for output.

Most C++ statements end in a semicolon, so it's important to get used to adding a semicolon if you come from a language such as BASIC, which ends statements at the end of a line, without any terminating punctuation such as semicolons. C++ does not care about spaces, tabs, or new lines (which together are called whitespace). Take a look at the following example, where the user input is spread across several lines:

;>   2
 +
 3 -
1;
(int)4

As with all computer ...

Get C++ By Example: UnderC Learning Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.