Reading input with ACCEPT
DISPLAY writes a line out; ACCEPT reads one in. Each ACCEPT reads the
next line of the program's standard input and stores it in a data item.
On z/OS that input is the job's SYSIN, typically a few lines typed
straight into the JCL:
//STEP01 EXEC PGM=GREET
//SYSIN DD *
MARY
/*
Batch programs use SYSIN for small control values: a run date, a branch
number, a "full or partial run" switch. Bulk data comes from files, which you
will meet later.
ACCEPT needs somewhere to put the data
You can't ACCEPT into a literal. You need a variable in
WORKING-STORAGE:
IDENTIFICATION DIVISION.
PROGRAM-ID. GREET.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-NAME PIC X(10).
PROCEDURE DIVISION.
ACCEPT WS-NAME.
DISPLAY "HELLO, " WS-NAME "!".
STOP RUN.
With the input MARY, this prints:
HELLO, MARY !
Why the gap? WS-NAME is PIC X(10): always exactly ten characters. The
four letters of MARY fill the left end and the rest is padded with
spaces. DISPLAY shows the whole field, spaces included. If the input
were longer than ten characters, the extra characters on the right would be
cut off. Fixed-length fields are fundamental to COBOL; the next module
covers them in detail.
A practical consequence: put a variable-length text field at the end of a line where you can, so the padding is invisible.
Reading numbers
For a number, use a numeric picture such as PIC 9(3) (three digits):
IDENTIFICATION DIVISION.
PROGRAM-ID. BRANCH.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-BRANCH PIC 9(3).
PROCEDURE DIVISION.
ACCEPT WS-BRANCH.
DISPLAY "BRANCH NUMBER " WS-BRANCH.
STOP RUN.
Input 42 produces BRANCH NUMBER 042. A numeric field always shows all
its digits, so leading zeros appear.
GnuCOBOL versus z/OS
GnuCOBOL converts 42 into the numeric field for you. IBM's compiler
copies the characters exactly as typed, left-justified, so on z/OS
SYSIN values for numeric fields are written at full width with
leading zeros (042). Do the same and your programs behave identically
on both.
GnuCOBOL also reads no more characters than the field has positions.
PIC 9(5)V99 is seven positions, so the input 12345.67 is read as
12345.6 and the last digit is lost. Make numeric input fields wide
enough for the text you type, decimal point included.
Several values, in order
Each ACCEPT consumes one line, top to bottom. You can read values in one
order and display them in another; they stay in their fields until you
change them.
Getting the date
ACCEPT can also read from the system instead of from input.
ACCEPT WS-TODAY FROM DATE YYYYMMDD fills a PIC 9(8) field with today's
date, for example 20260924:
IDENTIFICATION DIVISION.
PROGRAM-ID. TODAY.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-TODAY PIC 9(8).
PROCEDURE DIVISION.
ACCEPT WS-TODAY FROM DATE YYYYMMDD.
DISPLAY "RUN DATE: " WS-TODAY.
STOP RUN.
On the job
Production batch rarely trusts the system clock for the business date.
The date usually comes in through SYSIN or a control file, so a job
that runs after midnight still processes yesterday's business, and a
rerun next week uses the original date.
Your task
An operator logs on to the night shift. The job's input has two lines:
- the operator's name (up to 20 characters)
- the shift number (one digit)
Read both with ACCEPT into WS-OPERATOR and WS-SHIFT, then display:
SHIFT 2 LOGON
OPERATOR: JANE DOE
(for the input JANE DOE and 2). Note that the shift is printed first,
even though it is read second.