Mooophy / Cpp-Primer

C++ Primer 5 answers
Creative Commons Zero v1.0 Universal
8.11k stars 3k forks source link

Exercise 6.48 #756

Open haocheng6 opened 4 years ago

haocheng6 commented 4 years ago

I do not agree with that

the assert would be always true.

In this code, if no word input by the user matches sought, cin will be invalid after the loop, causing the expression cin to be false.

According to page 241,

Therefore, assert should be used only to verify things that truly should not be possible. It can be useful as an aid in getting a program dubugged but should not be used to substitute for run-time logic checks or error checking that the program should do.

As discussed above, it is definitely possible that cin may be evaluated as false after the loop, which is not a bug. So we should not use assert here. Instead, it should be within the program's logic to check if we have found a word matching sought, so an if statement will do.