Page 1 of 2

File Processing with COBOL.

PostPosted: Tue May 01, 2018 2:10 am
by erich
Trying to create this program with COBOL and having a hard time understanding the problem definition. Any help would be appreciated.


Create a COBOL program to update a master indexed file.

Notes:

A. Customer number is the key field for the indexed master file.
B. If a transaction record exits for which there is no corresponding master, display it as an error.
C. For all transaction records with corresponding master records (these are master records to be updated), add the amount of purchase from the transaction record to the amount owed in the master record and update the date of last purchase.
D. There need not to be a transaction record for each master record.
E. Transaction records are not in sequence.

Here are the record layouts.

CUSTOMER-TRANS
CUSTOMER-NO 5A
CUSTOMER-NAME 20A
DATE-OF-LAST-PURCHASE 8S
AMT-OF-PURCHASE 5S 2

CUSTOMER-MASTER
CUSTOMER-NO 5A
CUSTOMER-NAME 20A
DATE-OF-LAST-PURCHASE 8S
AMOUNT-OWED 6S 2

Re: File Processing with COBOL.

PostPosted: Tue May 01, 2018 2:19 am
by Robert Sample
It is not clear where you are having problems. The processing logic (pseudo-code) will be
Read transaction record
Do until end of transaction file
    Read master file by key
    If no master record with that key, transaction error
    Else do
           Update master record fields
           Rewrite master record
           End-do
    End-if
    Read transaction record
End-do

Re: File Processing with COBOL.

PostPosted: Tue May 01, 2018 2:24 am
by erich
For this particular problem, would I need to define and write any headings?

Re: File Processing with COBOL.

PostPosted: Tue May 01, 2018 3:05 am
by Robert Sample
Headings for what? Neither the master nor transaction files will have headings (and don't need them). Since you may have transaction errors, you may want to design a report layout for them, including headings. Then again, you may not -- it depends partly upon how complete you want the program to be and partly how much time you have for coding. If you use headings for the error reporting, you will want to ensure the headings are only printed if one (or more) errors occur -- there's nothing to generate questions like having headings without any detail lines (because none of the transactions failed to match master records)!

Re: File Processing with COBOL.

PostPosted: Thu May 03, 2018 11:00 pm
by erich
Still having some problems with this code.

Here is what I have:
-Physical file
-Logical file with customer-no as the key



COBOL code



 INPUT-OUTPUT SECTION.                                      
 FILE-CONTROL.                                              
     SELECT MASTER-FILE ASSIGN TO DATABASE-CUSTMASTR        
                    ORGANIZATION INDEXED                    
                    ACCESS MODE IS RANDOM                    
                    RECORD KEY IS EXTERNALLY-DESCRIBED-KEY  
                    WITH DUPLICATES.                        
     SELECT TRANS-FILE  ASSIGN TO DATABASE-CUSTTRANS        
                    ORGANIZATION IS SEQUENTIAL.              
     SELECT PRINT-LINE-OUT ASSIGN TO PRINTER-QSYSPRT.        
                                                             
  DATA DIVISION.                                            
  FILE SECTION.                                              
  FD MASTER-FILE.                                            
  01 MASTER-REC.                                            
     COPY DD-CUSTMASTRR OF CUSTMASTR.                        
                                                             
  FD TRANS-FILE.
 01 TRANS-REC.                                                      
    COPY DD-CUSTTRANSR OF CUSTTRANS.                                
                                                                   
 FD PRINT-LINE-OUT.                                                
 01 PRINT-LINE-OUT-REC             PIC X(80).                      
                                                                   
 WORKING-STORAGE SECTION.                                          
 01 FLAGS.                                                          
    05 ARE-THERE-MORE-RECS         PIC X(3)   VALUE 'YES'.          
       88 NO-MORE-RECORDS                     VALUE 'NO '.          
                                                                   
 01 HEADER1-OUT.                                                    
    05               PIC X(17)  VALUE 'ERROR REPORT FILE'.          
                                                                   
 01 ERROR-OUT.                                                      
    05               PIC X(24) VALUE 'ERROR WITH CUSTOMERNO - '.    
    05  CUSTNO-OUT   PIC X(5) VALUE SPACES.                        
                                                                   
 PROCEDURE DIVISION.                                                
 100-MAIN.
     OPEN INPUT TRANS-FILE, OUTPUT PRINT-LINE-OUT.            
   OPEN I-O MASTER-FILE.                                    
   PERFORM 200-HEADING-RTN.                                  
   PERFORM UNTIL NO-MORE-RECORDS                            
       READ TRANS-FILE AT END MOVE 'NO' TO ARE-THERE-MORE-REC
        NOT AT END PERFORM 200-MAIN-PROCESS                  
       END-READ                                              
   END-PERFORM.                                              
   CLOSE TRANS-FILE, MASTER-FILE, PRINT-LINE-OUT.            
                                                             
                                                             
 200-MAIN-PROCESS.                                          
    MOVE SPACES TO MASTER-REC.                              
    MOVE CUSTNO OF CUSTTRANSR TO CUSTNO OF CUSTMASTRR.      
    READ MASTER-FILE                                        
       INVALID KEY PERFORM 700-ERROR-RTN                    
       NOT INVALID KEY PERFORM 500-REWRITE-RTN              
    END-READ.

     200-HEADING-RTN.                                          
    WRITE PRINT-LINE-OUT-REC FROM HEADER1-OUT              
          AFTER ADVANCING 2 LINES.                          
    WRITE PRINT-LINE-OUT-REC FROM ERROR-OUT.                
                                                           
                                                           
 500-REWRITE-RTN.                                          
    MOVE DATEPURCH OF CUSTTRANS TO LASTPURCH OF CUSTMASTR.  
    ADD  AMTPURCH  OF CUSTTRANS TO AMTOWED   OF CUSTMASTR.  
        REWRITE MASTER-REC                                  
           INVALID KEY PERFORM 700-ERROR-RTN                
        END-REWRITE.                                        
                                                           
                                                           
 700-ERROR-RTN.                                            
    MOVE CUSTNO OF CUSTTRANS  TO CUSTNO-OUT.                
    MOVE CUSTNO OF CUSTMASTR  TO CUSTNO-OUT.                
    WRITE PRINT-LINE-OUT-REC FROM ERROR-OUT.                                                                                                                                                            











 

Re: File Processing with COBOL.

PostPosted: Thu May 03, 2018 11:37 pm
by Robert Sample
Still having some problems with this code.
Since you do not say what problem(s) you are having, do we guess?

Re: File Processing with COBOL.

PostPosted: Fri May 04, 2018 12:11 am
by erich
1. I'm not sure if my procedure division is doing what I want it to do.


2. I'm getting undefined variable errors. These variables are defined in the physical file.

'Datepurch of custtrans' not defined name.

For example:


500-REWRITE-RTN.                                          
    MOVE DATEPURCH OF CUSTTRANS TO LASTPURCH OF CUSTMASTR.  
    ADD  AMTPURCH  OF CUSTTRANS TO AMTOWED   OF CUSTMASTR.  
        REWRITE MASTER-REC                                  
           INVALID KEY PERFORM 700-ERROR-RTN                
        END-REWRITE.      
 

Re: File Processing with COBOL.

PostPosted: Fri May 04, 2018 1:04 am
by Robert Sample
1. Running the code without run-time errors will show you any problems with the PROCEDURE DIVISION.
2. Undefined variables are real simple to resolve -- look at the copybook to see what the variable name REALLY is, and change your code to match the copybook.
These variables are defined in the physical file.
What on earth does this mean? COBOL variables are defined IN THE PROGRAM and only in the program -- there is no such thing, on a mainframe, as "variables (are) defined in the physical file". Data sets (the only files on a mainframe are on tape or in Unix) contain data, not variable names. The same data may mean different things, depending upon how it is defined. For example, X'0097994D' will be '<low value>pr(' if treated as character, -97994 if treated as a packed decimal value, or 9,935,181 if treated as a binary (COMP / COMP-5) value. How to know the difference? By how the program defines those bytes!

Re: File Processing with COBOL.

PostPosted: Sat May 05, 2018 3:44 am
by erich
Robert,

I am getting an I/O error when I try to call my program. Any help would be appreciated.

Two Physical files: custmastr
custtrans

Logical file: custlogic1

Here is my code:


 FILE-CONTROL.                                          
     SELECT MASTER-FILE ASSIGN TO DATABASE-CUSTLOGIC    
                                ORGANIZATION INDEXED    
                                ACCESS MODE IS RANDOM  
                    RECORD KEY IS  CUSTNO OF MASTER-REC.
     SELECT TRANS-FILE  ASSIGN TO DATABASE-CUSTTRANS    
                          ORGANIZATION IS SEQUENTIAL.  
     SELECT PRINT-LINE-OUT ASSIGN TO PRINTER-QSYSPRT.  
                                                       
  DATA DIVISION.                                        
  FILE SECTION.                                        
  FD MASTER-FILE.                                      
  01 MASTER-REC.                                        
     COPY DD-CUSTLOGICR OF CUSTLOGIC.                  
                                                       
  FD TRANS-FILE.                                        
  01 TRANS-REC.
   01 TRANS-REC.                                              
    COPY DD-CUSTTRANSR OF CUSTTRANS.                        
                                                           
 FD PRINT-LINE-OUT.                                        
 01 PRINT-LINE-OUT-REC             PIC X(80).              
                                                           
 WORKING-STORAGE SECTION.                                  
 01 FLAGS.                                                  
    05 ARE-THERE-MORE-RECS         PIC X(3)   VALUE 'YES'.  
       88 NO-MORE-RECS                        VALUE 'NO '.  

  01 HEADER1.                                                    
     05                          PIC X(5)  VALUE 'CUSTNO'.      
     05                          PIC X(3)  VALUE SPACES.        
     05                          PIC X(20) VALUE 'CUSTOMER NAME'.
     05                          PIC X(3)  VALUE SPACES.        
     05                          PIC X(10) VALUE 'LAST DATE'.    
     05                          PIC X(3)  VALUE SPACES.        
     05                          PIC X(11) VALUE 'AMOUNT OWED'.  
     05                          PIC X(3)  VALUE SPACES.        
     05                          PIC X(7)  VALUE 'MESSAGE'.      
  01 DETAIL-LINE.                                                
     05  CUSTNO-OUT              PIC 9(5).                      
     05                          PIC X(3)  VALUE SPACES.        
     05  CUSTNA-OUT              PIC X(20).                      
     05                          PIC X(3)  VALUE SPACES.        
     05  DATE-OUT                PIC 9(8).                      
     05                          PIC X(6)  VALUE SPACES.        
     05  AMT-OWED-OUT            PIC Z,ZZZ.99.                  
     05                          PIC X(3)  VALUE SPACES.        
     05  MESSAGE-OUT             PIC X(30).                      
                                                                 

                                                 
  PROCEDURE DIVISION.                            
  100-MAIN-MODULE.                                
     PERFORM 800-INITIALIZATION-RTN.              
     PERFORM UNTIL NO-MORE-RECS                  
       READ TRANS-FILE                            
            AT END                                
              MOVE 'NO' TO ARE-THERE-MORE-RECS    
            NOT AT END                            
              PERFORM 400-UPDATE-RTN              
       END-READ                                  
     END-PERFORM.                                
     PERFORM 900-END-OF-JOB-RTN.                  
     STOP RUN.  

 400-UPDATE-RTN.                                              
   MOVE SPACES TO MASTER-REC.                                
   READ MASTER-FILE                                          
        INVALID KEY PERFORM 700-ERROR-RTN                    
        NOT INVALID KEY PERFORM 500-REWRITE-RTN              
   END-READ.                                                  
                                                             
 500-REWRITE-RTN.                                            
   MOVE CUSTNO OF MASTER-REC TO CUSTNO-OUT.                  
   MOVE CUSTNAME OF MASTER-REC TO CUSTNA-OUT.                
   MOVE LASTPURCH OF MASTER-REC TO DATE-OUT.                  
   MOVE AMTOWED OF MASTER-REC TO AMT-OWED-OUT.                
   MOVE 'BEFORE' TO MESSAGE-OUT.                              
   WRITE PRINT-LINE-OUT-REC FROM DETAIL-LINE.                
   ADD AMTPURCH   OF TRANS-REC TO AMTOWED   OF MASTER-REC.    
   MOVE DATEPURCH OF TRANS-REC TO LASTPURCH OF MASTER-REC    
                                                 DATE-OUT.    
  MOVE AMTOWED OF MASTER-REC TO AMT-OWED-OUT.            
  MOVE 'AFTER' TO MESSAGE-OUT.                          
  WRITE PRINT-LINE-OUT-REC FROM DETAIL-LINE.            
  REWRITE MASTER-REC                                    
     INVALID KEY PERFORM 700-ERROR-RTN                  
  END-REWRITE.                                          
                                                         
 700-ERROR-RTN.                                          
     MOVE CUSTNO OF TRANS-REC TO CUSTNO-OUT.            
     MOVE 'CUSTNO NOT FOUND' TO MESSAGE-OUT.            
     MOVE CUSTNAME OF TRANS-REC TO CUSTNA-OUT.          
     MOVE DATEPURCH OF TRANS-REC TO DATE-OUT.            
     MOVE AMTPURCH OF TRANS-REC  TO AMT-OWED-OUT.        
     WRITE PRINT-LINE-OUT-REC FROM DETAIL-LINE.          

                                             
 800-INITIALIZATION-RTN.                    
     OPEN INPUT TRANS-FILE                  
          I-O   MASTER-FILE.                
     OPEN OUTPUT PRINT-LINE-OUT.            
     WRITE PRINT-LINE-OUT-REC FROM HEADER1.  
                                             
 900-END-OF-JOB-RTN.                        
     CLOSE TRANS-FILE                        
           PRINT-LINE-OUT                    
           MASTER-FILE.                                                       
                                                                                          




 

Re: File Processing with COBOL.

PostPosted: Sat May 05, 2018 6:04 am
by NicC
What i/o error? Show it to us.