Simplifying by Using a bool Variable

The big danger in the earlier example is that, because you've put the same bool expression in two places, you will cause an accident if you do not make or keep the two expressions identical. This could lead to a serious and hard-to-solve problem in your program during repair or enhancement. You can use a bool variable to avoid it, as shown in Listing 10.3.

Listing 10.3. UserWantsToContinueYOrN in PromptModule with a bool Expression
 1: bool UserWantsToContinueYOrN (const char *theThingWeAreDoing) 2: { 3: char DoneCharacter; *4: bool InvalidCharacterWasEntered = false; 5: 6: do 7: { 8: cout << 9: endl << 10: theThingWeAreDoing << 11: " - Press \"n\" and \"Enter\" to stop: "; 12: 13: cin >> DoneCharacter; 14: ...

Get SAMS Teach Yourself C++ in 10 Minutes SECOND 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.