I do not know for sure if this is a problem, but I see a logical stream that will not work very well ...
First: 400-SALESMAN-NAME reads seller records from a file into the SALESMAN-TABLE storage SALESMAN-TABLE .
The file probably looks something like this:
01Sales Guy One 02Lance Winslow 03Scott Peterson 04Willy Loman
When the read loop is completed, SALESMAN-NUMBER will be equal to the index of the table due to how you load the table (using SM-NUMBER-IN to set the index of the table). Not a problem yet ...
Next: 500-PROCESS-FILE scrolls all the lines in SALESMAN-TABLE by running the ROUTINE-CHECK index from 1 to 99 and executing 510-TABLE-SEARCH to write a report for the seller, where the index is SALESMAN-NUMBER ...
Next: the SEARCH statement. Everything here is strange and never performs 520-WRITE-FILE . That's why.
The SEARCH operator implements a linear search ( SEARCH ALL - binary search). SEARCH simply increments the index associated with the table you are looking for and then goes through the WHEN test suite until one of them fries or the index expires from the end of the table. The index for your TABLE-ENTRIES is IND-TABLE-ENTRIES . But you never install or refer to it (this is the root of the problem). I will explain in a moment ...
Note that the WHEN part of your SEARCH uses the ROUTINE-CHECK index. ROUTINE-CHECK was set to 500-PROCESS-FILE . Also note that you only get 520-WRITE-FILE if SALESMAN-NUMBER matches the ROUTINE-CHECK value - which it will do if the seller from this number was read from the input file. This might work because you loaded the table so that the line number was equal to the seller number in 450-TABLE-LOAD .
Now, what happens if the input file does not contain a seller, where SM-NUMBER-IN is 01?
Pass through it, strike a blow ...
ROUTINE-CHECK set to 1, SEARCH is also called because the IND-TABLE-ENTRIES associated with the found table is less than the number found in the table (it was initialized to zero when the program was loaded). WHEN are WHEN .
The first test is WHEN SALESMAN-NUMBER (ROUTINE-CHECK) = ROUTINE-CHECK . Since Salesman 1 does not exist, SALESMAN-NUMBER will be zero and the test will fail (0 <> 1).
The following WHEN clause is checked, and it succeeds because (0 = 0); but this is the "do nothing" option, so another SEARCH loop is introduced after IND-TABLE-ENTRIES incremented .
The same results and all subsequent iterations through the SEARCH ed WHEN list (none of the sentences match) ... Repeat this cycle until IND-TABLE-ENTRIES is expanded outside the table.
At this point, the SEARCH terminates and control returns to the next cycle in the 500-PROCESS-FILE . Nothing was printed.
500-PROCESS-FILE then increments ROUTINE-CHECK by 1 (now 2). We have a seller with SALESMAN-NUMBER of 02, so we should get some result - right? Wrong! But why?
If you read the verb SEARCH , you will find that it does not reset the table index (in this case: IND-TABLE-ENTRIES ). He begins to use whatever value he has when entering SEARCH. You never reset, so it is already installed outside the table. The SEARCH just ends and nothing is printed - again and again.
Problem fixing
Given that you downloaded TABLE-ENTRIES by seller number in the first place, I donβt see the purpose of using SEARCH. Just do something like:
500-PROCESS-FILE. PERFORM VARYING ROUTINE-CHECK FROM 1 BY 1 UNTIL ROUTINE-CHECK > 99 IF SALESMAN-NUMBER (ROUTINE-CHECK) = ZERO CONTINUE ELSE PERFORM 520-WRITE-FILE END-IF END-PERFORM.
It would also be nice to have an initialization cycle for the table so that each SALESMAN-NUMBER is explicitly set to zero before reading the seller file.
If you must use SEARCH in this program, be sure to set and use the associated table index variable when referencing a regular table.