Skip to content

Files

Latest commit

 

History

History

04

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

Exercise 5.04

Explain each of the following examples, and correct any problems you detect.

(a) while (string::iterator iter != s.end()) { /* ... */ }
(b)

while (bool status = find(word)) { /* ... */ }
if (!status) { /* ... */ }

Solution

(a) The loop attempts to traverse the string s, but fails to initialize the iterator iter:

for (string::iterator iter = s.begin(); s != s.end(); ++iter) { /* ... */ }

(b) The if statement after the loop attempts to access status which is out of scope. The loop itself terminates when find(word) returns false.

bool status
while (status = find(word)) { /* ... */ }
if (!status) { /* ... */ }