Chapter 6

A1: Both versions give the same answers, but the if else version is more efficient. Consider what happens, for example, when ch is a space. Version 1, after incrementing spaces, tests to see whether the character is a newline. This wastes time because the program already has established that ch is a space and hence could not be a newline. Version 2, in the same situation, skips the newline test.
A2: Both ++ch and ch + 1 have the same numerical value. But ++ch is type char and prints as a character, while ch + 1, because it adds a char to an int, is type int and prints as a number.
A3: Because the program uses ch = `$' instead of ch == `$', the combined input and output looks like this:
 Hi! H$i$!$ $Send $10 or $20 now! S$e$n$d$ $ct1 = ...

Get The Waite Group's C++ Primer Plus, Third 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.