|
|
|
|
|
|
|
We leave the coding of this driver program and the creation of the test data as an exercise (see Programming Problem 6). Functions Search and SelSort should be tested in a similar manner. |
|
|
|
|
|
|
|
|
At the beginning of this section, we said that three of the functions used in this chapter had been tested in the Exam program. However, to make sure that they stand up as general-purpose functions, they should be subjected to the same rigorous testing proposed for Search, Search2, and SelSort. |
|
|
|
|
|
|
|
|
In two of the search functions (Search2 and SearchOrd), we stored the value being searched for in list[length]. The assumption that length would be less than MAX_LENGTH (the number of places in the array) is written into the documentation. This could lead to a potential error if the calling module fails to check that length is less than MAX_LENGTH. |
|
|
|
|
|
|
|
|
If the precondition listed in the function documentation is that length < MAX_LENGTH, then the calling module must make sure that the function is not called with length > MAX_LENGTH. Another way of dealing with the case where length > MAX_LENGTH is to add an error flag to the functions' formal parameter lists and have the functions themselves check for length > MAX_LENGTH. If length > MAX_LENGTH, the error flag is set to TRUE and the search is terminated. This approach changes the preconditions of the functions so that they can be called even when length > MAX_LENGTH. |
|
|
|
|
|
|
|
|
Either way of handling the problem is acceptable. The important point is that it must be clearly stated whether the calling routine or the called function is to check for the error condition. |
|
|
|
|
|
|
|
|
Testing and Debugging Hints |
|
|
|
|
|
|
|
|
1. Review the Testing and Debugging Hints for Chapter 11. They apply to all one-dimensional arrays, including strings. |
|
|
|
|
|
|
|
|
2. Make sure that every string is terminated with the null character. String constants are automatically null-terminated by the compiler. On input, the >> operator and the get function automatically add the null character. If you store characters into a string variable individually or manipulate the array in any way, be sure to account for the null character. |
|
|
|
|
|
|
|
|
3. Remember that C++ treats string initialization (in a declaration) as different from string assignment. Initialization is allowed but assignment is not. |
|
|
|
|
|
|
|
|
4. Aggregate input/output is allowed for strings but not for other array types. |
|
|
|
|
|
|
|
|
5. If you use the >> operator to input into a string variable, be sure the array is large enough to hold the null character plus the longest sequence of (nonwhitespace) characters in the input stream. |
|
|
|
|
|
|
|
|
6. With string input, the >> operator stops at, but does not consume, the first trailing whitespace character. Likewise, if the get function stops reading early because it encounters a newline character, the newline character is not consumed. |
|
|
|
|
|
|
|
|
7. When you use the strcpy library function, ensure that the destination array is at least as large as the array from which you are copying. |
|
|
|
|
|