Thursday, 14 August 2014

COBOL Terminal Input and Output: ACCEPT and DISPLAY Examples

COBOL terminal input output flow using ACCEPT and DISPLAY
ACCEPT reads input. DISPLAY writes output.

A small test job asks for an account number, the operator types one line, and the program prints the value to the spool. In COBOL, that simple exchange is usually built with two verbs: ACCEPT for input and DISPLAY for output.

This page explains terminal input and output in COBOL in plain English. It focuses on what a beginner sees in a batch job, TSO test, or training program: where the input comes from, where the message goes, and why these verbs should be used carefully in production code.

What Terminal Input and Output Means in COBOL

Terminal input and output means the program communicates with a person or a simple run-time device instead of reading a normal business file. In a mainframe batch job, input can come from SYSIN. Output often goes to SYSOUT, the job log, or a console-related destination depending on the compile and run-time setup.

That is different from file processing. A file program normally uses OPEN, READ, WRITE, and CLOSE with an FD entry. Terminal I/O is simpler, but it is not a replacement for structured file design. Use it for prompts, diagnostics, small utilities, classroom examples, and controlled test jobs.

ACCEPT Verb in COBOL

The ACCEPT statement moves incoming data into a COBOL data item. IBM documents that ACCEPT does not validate or edit the incoming value for you, so the program must check the field after reading it. If the user enters letters into a numeric account field, your program needs to decide what to do.

Simple ACCEPT Syntax

WORKING-STORAGE SECTION.
01  WS-ACCOUNT-NO        PIC X(10).
01  WS-REPLY             PIC X.

PROCEDURE DIVISION.
    DISPLAY "ENTER ACCOUNT NUMBER: ".
    ACCEPT WS-ACCOUNT-NO.

    DISPLAY "ACCOUNT ENTERED: " WS-ACCOUNT-NO.
    STOP RUN.

In a simple batch test, the value accepted by the program can be supplied through SYSIN. That lets a developer repeat the same test without typing at a screen every time.

Batch Test with SYSIN

//STEP01   EXEC PGM=ACCTIO
//STEPLIB  DD DISP=SHR,DSN=PROD.COBOL.LOAD
//SYSOUT   DD SYSOUT=*
//SYSIN    DD *
ACCT000123
/*

When the program executes ACCEPT WS-ACCOUNT-NO, the run-time environment can read the next available input line from the assigned input source. Site standards vary, so always check how your shop maps COBOL terminal input in batch, TSO, and z/OS UNIX runs.

DISPLAY Verb in COBOL

The DISPLAY statement writes literals and data items to an output device. With no special destination phrase, the output normally goes to the system logical output device. In a batch job, that usually means the message appears in spool output associated with SYSOUT.

Simple DISPLAY Syntax

01  WS-ACCOUNT-NO        PIC X(10) VALUE "ACCT000123".
01  WS-STATUS            PIC X(08) VALUE "ACTIVE".

PROCEDURE DIVISION.
    DISPLAY "ACCOUNT=" WS-ACCOUNT-NO
            " STATUS=" WS-STATUS.
    STOP RUN.

This is useful for small trace messages while testing. For example, a developer may display the key before a READ, then display the file status after the READ. For long-running production jobs, permanent logging rules should come from your team standard rather than scattered DISPLAY statements.

ACCEPT and DISPLAY Together

The pair is easy to understand when shown together. The program below reads an employee number and prints a clean response. It also checks that the field is not blank before continuing.

IDENTIFICATION DIVISION.
PROGRAM-ID. TERMIO.

DATA DIVISION.
WORKING-STORAGE SECTION.
01  WS-EMP-NO            PIC X(08).

PROCEDURE DIVISION.
    DISPLAY "ENTER EMPLOYEE NUMBER: ".
    ACCEPT WS-EMP-NO.

    IF WS-EMP-NO = SPACES
        DISPLAY "EMPLOYEE NUMBER IS REQUIRED"
    ELSE
        DISPLAY "EMPLOYEE NUMBER ENTERED: " WS-EMP-NO
    END-IF.

    STOP RUN.

The validation is deliberately simple. In a real payroll or claims program, you might also check length, allowed characters, and whether the key exists in a VSAM file or Db2 table.

Common Uses in Mainframe Work

Use case Typical verb Example
Read one test value ACCEPT Read an account number from SYSIN.
Print a diagnostic message DISPLAY Show WS-FILE-STATUS after a failed read.
Confirm selected options DISPLAY Print the run date, region, or control-card value.
Build a quick training program Both Prompt for a value, accept it, and display the result.

Validation Rules Beginners Often Miss

ACCEPT is not the same as business validation. It reads the data, but it does not prove the data is right. For a numeric field, accept into an alphanumeric work field first when bad input is possible, check it, and only then move it to a numeric field.

01  WS-AMOUNT-TEXT       PIC X(07).
01  WS-AMOUNT            PIC 9(07).

PROCEDURE DIVISION.
    DISPLAY "ENTER AMOUNT: ".
    ACCEPT WS-AMOUNT-TEXT.

    IF WS-AMOUNT-TEXT IS NUMERIC
        MOVE WS-AMOUNT-TEXT TO WS-AMOUNT
        DISPLAY "AMOUNT ACCEPTED: " WS-AMOUNT
    ELSE
        DISPLAY "AMOUNT MUST BE NUMERIC"
    END-IF.

This avoids a common beginner mistake: reading untrusted input directly into a numeric field and then wondering why the program fails later or prints confusing output.

DISPLAY Output Limits and Formatting

Keep displayed lines short. IBM documents device-related limits for displayed records, and console output is especially limited compared with ordinary spool output. A safe training habit is to display concise messages and split long diagnostic text across several lines.

DISPLAY "INPUT ACCOUNT: " WS-ACCOUNT-NO.
DISPLAY "FILE STATUS  : " WS-FILE-STATUS.
DISPLAY "ACTION       : CHECK CUSTOMER MASTER".

Avoid one huge DISPLAY line containing every field in working storage. It is hard to read in spool output, and it can be split by the output device. Short messages are easier to scan during an abend review.

ACCEPT vs READ in COBOL

Use ACCEPT when the program needs a small value from a simple input source. Use READ when the program processes records from a defined file. A batch payment program should not use ACCEPT to process thousands of payment records; it should define the input file in the Environment Division and read it record by record.

For file processing topics, continue with the Mainframe Forum guides on COBOL READ statement, COBOL file status, and COBOL OPEN statement.

How This Fits with Other COBOL Verbs

ACCEPT and DISPLAY are often the first COBOL I/O verbs a learner sees. After that, the next step is usually file input and output, then table processing, string handling, and conditional logic. Related Mainframe Forum notes include COBOL ACCEPT statement, COBOL DISPLAY statement, COBOL PERFORM statement, and COBOL data types.

Common Mistakes

Accepting Bad Data Without a Check

If a field must be numeric, test it before moving it to a numeric target. If the field must be a valid code, compare it with known values or a control table.

Using DISPLAY as a Permanent Audit Trail

DISPLAY is fine for training and controlled diagnostics. For production audit evidence, use the logging method approved for the application so operations teams can search and retain it properly.

Mixing Terminal I/O with File I/O

Do not use ACCEPT for normal transaction files just because it looks shorter than READ. File I/O gives you record definitions, file status checks, and clearer restart behavior.

FAQ

What is the COBOL terminal input verb?

The common terminal input verb is ACCEPT. It reads data from an input source and places it into the named COBOL data item.

What is the COBOL terminal output verb?

The common terminal output verb is DISPLAY. It writes literals or data-item values to the output destination used by the run-time environment.

Can ACCEPT validate input automatically?

No. ACCEPT reads the input value, but the program must validate it. Use checks such as IS NUMERIC, blank checks, valid-code checks, or file/table lookups.

Should production batch programs use ACCEPT for large input files?

No. Large business input should usually be handled with normal file processing: OPEN, READ, file status checks, and CLOSE.

References

For exact syntax and current compiler behavior, use IBM documentation for Enterprise COBOL ACCEPT statement, Enterprise COBOL DISPLAY statement, and assigning input from a screen or file.

Use ACCEPT for small controlled input, use DISPLAY for readable messages, and validate every value that can come from outside the program.

No comments:

Post a Comment

New In-feed ads