How do I prevent internal table overflow in COBOL?
<p>This is probably a rookie question, but I still have a hard time working out how to use tables in COBOL.</p>

<p>Currently I'm writing a simple module that can be called by other programs.
My program has to search an internal table for data and if not found, add to it.</p>

<p>I have this table: </p>

<pre><code>01 TB-1 OCCURS 10 INDEXED X1.
   03 CLIENT-NAME    PIC N(30).
   03 ORDER-NUMBER   PIC 9(06).
</code></pre>

<p>Of course I can increase the amount of OCCURS so the likelihood of table overflow decreases. However, when testing the module, I have to deal with the scenario that there's one more record to add that doesn't fit in the table.</p>

<p>What is the best way of dealing with this? I was thinking of preventing an ABEND by giving back an error message. </p>

<p>To do this, I thought I'd define a new field and using it as a counter. So basically, every time my module adds a record, it adds +1 to the counter as well. Kind of like this:</p>

<pre><code>IF COUNTER &lt; 10 
     PERFORM ADD-RECORD
ELSE DISPLAY 'INPUT HAS EXCEEDED MAX OF 10 OCCURRENCES'
     GOBACK 
END-IF
.

ADD-RECORD.
    MOVE INPUT-CLIENT-NAME  TO CLIENT-NAME(X1)  IN TB-1.
    MOVE INPUT-ORDER-NUMBER TO ORDER-NUMBER(X1) IN TB-1.
    ADD +1 TO COUNTER
    .
</code></pre>

<p>Is this a good way to do this? Do you have any other ideas? Thanks in advance for your help. </p>