ACCEPT and DISPLAY

COBOL began as a batch language, and its two statements for a conversation are the oldest ones it has: DISPLAY writes to the terminal and ACCEPT reads a line from it. A modern program uses them for a console tool, for a diagnostic, and for the values of the clock.

DISPLAY without a newline

Every DISPLAY in this course ended its line. The clause WITH NO ADVANCING suppresses the line end, so two statements write one line, which is how a prompt is made:

IDENTIFICATION DIVISION. PROGRAM-ID. LINE. DATA DIVISION. WORKING-STORAGE SECTION. 01 COUNT-F PIC 9(3) VALUE 42. PROCEDURE DIVISION. DISPLAY "Reading " WITH NO ADVANCING. DISPLAY COUNT-F " records" WITH NO ADVANCING. DISPLAY " ...". DISPLAY "done". STOP RUN.

cobc -x -free -Wall -o line line.cob
./line

Reading 042 records ...
done

The first three statements wrote one line, and the fourth started a new one. The field is printed in its picture width, 042, because DISPLAY writes the bytes of the field and not the number a reader would write.

ACCEPT reads a line

ACCEPT takes one field, reads a line from the standard input and moves it into the field with the rules of a MOVE: the line goes in at the left, the rest of the field is spaces, and a line longer than the field is cut. The listing below is the procedure division of a program whose data division declares NAME-F and CITY-F as PIC X(20) and whose identification division names it GREET:

PROCEDURE DIVISION. DISPLAY "Your name: " WITH NO ADVANCING. ACCEPT NAME-F. DISPLAY "Your city: " WITH NO ADVANCING. ACCEPT CITY-F. DISPLAY "Hello " FUNCTION TRIM(NAME-F) " of " FUNCTION TRIM(CITY-F) "!". STOP RUN.

This block is a fragment and the checker does not build it, because a program that waits for a terminal cannot be run without one. The run below is real, with the two lines piped into the program:

printf "Ada\nLondon\n" | ./greet

Your name: Your city: Hello Ada of London!

The prompt appears, the cursor waits, and the answer arrives on the same line because the prompt had no line end. The two answers are trimmed before they are printed, because each field is twenty bytes and the spaces would otherwise sit in the middle of the sentence.

What ACCEPT does at the end of the input

A console program can be run with no input at all, and then ACCEPT returns immediately with an empty line. The line is moved into the field like any other, so an alphanumeric field becomes spaces and whatever it held before is gone:

./greet < /dev/null

Your name: Your city: Hello of !

Two prompts, no answer, and a sentence with two empty places in it. This is the failure a program under a scheduler shows when nobody is at the terminal, and it is why a program that needs a name from a terminal tests the field after the ACCEPT before it uses it. The end of the input is not an error the language reports; it is an empty line.

ACCEPT FROM DATE and TIME

ACCEPT also reads values the system knows. ACCEPT TODAY-F FROM DATE fills an eight-digit field with the date as YYYYMMDD, and ACCEPT NOW-F FROM TIME fills eight digits with hours, minutes, seconds and hundredths. Both fields were checked with LENGTH OF: eight bytes each.

Neither value can be shown as a fixed line on this page, because the answer changes every second the page is read. That is not a limitation of the statement but of a printed example, and it is the reason the course prints the lengths and leaves the values to the lesson on dates and times, where a program computes with them.

This is the last lesson of the text part. The next part is control flow: IF and ELSE, the level 88 conditions that the data division has been preparing since lesson 12, and the EVALUATE that replaces a chain of them.

Exercises

  1. Print a line of ten dashes with three DISPLAY statements, one of them without the advancing clause. How many lines appear?
  2. Add a third question to the greeting program and run it with three piped lines. What does the prompt line look like?
  3. Run the greeting program with a line longer than twenty characters. What arrives in the field, and what is lost?
  4. Accept the date into a PIC 9(8) field and print it with slashes between the parts using reference modification. What is the date of the run?

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q0. DATA DIVISION. WORKING-STORAGE SECTION. 01 AMT-F PIC 9(3)V99 VALUE 7.5. PROCEDURE DIVISION. DISPLAY "Total: " WITH NO ADVANCING. DISPLAY AMT-F. STOP RUN.

    Answer

    Total: 007.50

    The clause keeps the two writes on one line, and the amount prints in the full width of its picture.

  2. How does ACCEPT move the line into its field?

    Answer

    Like a MOVE: from the left, padded with spaces, cut if too long

    The line is alphanumeric input, so the ordinary move rules apply and short lines leave spaces on the right.

  3. What is in the field when ACCEPT finds the end of the input?

    Answer

    Spaces: the empty line is moved into it and the old value is lost

    The end of the input behaves like an empty line, and the move rules write spaces into an alphanumeric field.

  4. What does ACCEPT ... FROM DATE put in an eight-digit field?

    Answer

    The date as YYYYMMDD

    The DATE value of the system is the calendar date in year, month, day order, eight digits in all.

  5. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q1. DATA DIVISION. WORKING-STORAGE SECTION. 01 COUNT-F PIC 9(3) VALUE 42. PROCEDURE DIVISION. DISPLAY "Reading " WITH NO ADVANCING. DISPLAY COUNT-F " records" WITH NO ADVANCING. DISPLAY " ...". STOP RUN.

    Answer

    Reading 042 records ...

    The two clauses keep both writes on one line, and the field prints its three digits without a separator.

  6. A program accepts a name with no prompt and the user types it. What appears on the screen?

    Answer

    Only the typed line, because nothing was printed first

    ACCEPT does not print anything itself; without a DISPLAY before it the cursor sits at the start of an empty line.

  7. What does this program print when its input is empty? IDENTIFICATION DIVISION. PROGRAM-ID. Q2. DATA DIVISION. WORKING-STORAGE SECTION. 01 NAME-F PIC X(6) VALUE SPACES. 01 PART-F PIC X(3) VALUE "ABC". PROCEDURE DIVISION. ACCEPT NAME-F. ACCEPT PART-F. DISPLAY "[" NAME-F "][" PART-F "]". STOP RUN.

    Answer

    [ ][ ]

    Each accept moves an empty line, so the second one replaces the ABC that the field started with by three spaces.

  8. What does this program print? IDENTIFICATION DIVISION. PROGRAM-ID. Q3. DATA DIVISION. WORKING-STORAGE SECTION. 01 WORD-F PIC X(10) VALUE "ok". PROCEDURE DIVISION. DISPLAY "[" WORD-F "]" WITH NO ADVANCING. DISPLAY " done". STOP RUN.

    Answer

    [ok ] done

    The field prints its ten bytes and the clause keeps the next write on the same line, with no separator added.

  9. A program accepts a line longer than its thirty-byte field. What happens?

    Answer

    The first thirty characters are kept and the rest of the line is lost

    ACCEPT follows the move rules, so the field takes what fits and the rest of the line is discarded silently.

  10. Why can a page not show the output of ACCEPT FROM TIME as a fixed line?

    Answer

    The value is the system clock, so it changes every time the program runs

    A printed example has to be reproducible, and a clock reading is not; the page checks the field length instead.

The code on this page was built with cobc (GnuCOBOL) 3.2.0 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.

Article author: Arthur Isaev