Search code examples
infinite-loopcobol

Cobol: Cannot Find What Is Causing Infinite Loop Error in File Matching


I have been trying to learn about file matching through school. When going through my code I get an infinite loop but I am not exactly sure what is causing it. All I know it is somewhere in my file matching portion of code.

01 Procedure Division.  
02   A-Para.
03      Perform Process-Para Until EOFa = "Y"
04        and EOFb = "Y"
05   Process-Para.  
06       Add 1 to counter
07       
08       If inFileSorted-empNum = timeFileSorted-empNum
09           Perform check-Para
10       End-If.
11   
12   check-Para. 
13       Perform ReadTimeFile
14       If changeEmployee equals "Y"
15           Perform Compute-Para
16       End-If
17       If timeFileSorted-empNum not equal to inFileSorted-empNum
18           Perform ReadinFile
19           If changeEmployee equals "Y"
20               Perform Compute-Para
21           End-If
22       End-If
23       
24       Perform 510-Calculate
25       Perform Display-Para.
26
27    ReadTimeFile. 
28       Move spaces to changeEmployee
29       
30       If not EOFb = "Y"
31           Move timeFileSorted-empNum to oldEmpNum              
32           Read timeFileSort             
33           At end
34               Move "Y" to EOFb
35               Move high-value to timeFileSorted-empNum
36           Not at end
37               Move timeFileSorted-empNum to newEmpNum  
38               If (oldEmpNum not equal newEmpNum)
39                   Move "Y" to changeEmployee
40               End-If
41           End-Read
42       End-If.  
43
44    ReadinFile.  
45       Move spaces to changeEmployee
46       
47       If not EOFa = "Y"
48           Move inFileSorted-empNum to oldEmpNum
49           Read Lab10-sort-File
50           At end
51               Move "Y" to EOFa
52               Move high-value to inFileSorted-empNum                  
53           Not at end
54               Move inFileSorted-empNum to newEmpNum  
55               If (oldEmpNum not equal newEmpNum)
56                  Move "Y" to changeEmployee
57               End-If
58           End-Read
59       End-If.

Could someone help me find what could be causing this problem? Any help would be greatly appreciated.


Solution

  • One possible cause (in Process-Para) is

    08       If inFileSorted-empNum = timeFileSorted-empNum
    09           Perform check-Para
    10       End-If.
    

    if inFileSorted-empNum not = timeFileSorted-empNum it will skip over check-Para and not read either file


    better option

    This is not intended to work but to show you how to structure the code. Please note at least one file is read each time through the loop

    open infile, timefile
    Perform read-inFile
    Perform read-timeFile
    perform until EOFa = "Y" or EOFb = "Y"
        evaluate true
        when inFileSorted-empNum < timeFileSorted-empNum
            ...
            Perform read-inFile
        when inFileSorted-empNum > timeFileSorted-empNum
            ...
            Perform read-timeFile
        when Other
           ...
            Perform read-timeFile  ???
        end-evaluate
    end-perform
    
    perform until EOFa = "Y"
        ...
    end-perform
    
    perform until EOFb not = "Y"
        ...
    end-perform
    .