Mooophy/Cpp-Primer

Exercise 6.48

haocheng6 opened this issue · 0 comments

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.