O'Reilly logo

SAMS Teach Yourself C++ in 10 Minutes SECOND EDITION by Jesse Liberty

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required