Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 38 ยท Procedures and Control Flow

ACCEPT and DISPLAY: Input and Output

Learn COBOL console I/O: DISPLAY with several operands, NO ADVANCING, UPON, ACCEPT from the keyboard, and ACCEPT FROM DATE, TIME and DAY-OF-WEEK.

The procedure division is where the program does things. Two verbs get you started: DISPLAY writes to the screen and ACCEPT reads from the keyboard or from the system (date, time, command-line arguments). Real batch programs read files, not keyboards, but console I/O is how you test, debug and write small utilities.

DISPLAY

DISPLAY writes one or more operands, back to back, followed by a newline:

COBOL
           DISPLAY "Hello"
           DISPLAY "Name: " CUST-NAME " Balance: " CUST-BALANCE
           DISPLAY "Line one" X"0A" "Line two"
  • Operands can be literals, data items and figurative constants. They are joined with no separator, so include spaces inside the literals.
  • Numeric items print as stored: digits only, no decimal point, with the sign overpunched into the last digit for signed items. Move the value to an edited field (see MOVE and edited pictures) to print it nicely.
  • X"0A" is a line feed character if you need extra line breaks.

Useful options:

COBOL
           DISPLAY "Enter your name: " WITH NO ADVANCING
           DISPLAY "Error: file missing" UPON SYSERR
           DISPLAY "Total" UPON CONSOLE
  • WITH NO ADVANCING suppresses the newline so a prompt and the answer share a line.
  • UPON SYSERR sends the text to the error stream. On the mainframe UPON SYSOUT / SYSERR map to job output.

ACCEPT from the keyboard

COBOL
       01 USER-NAME    PIC X(20).
       01 AGE          PIC 99.
       ...
           DISPLAY "Name? " WITH NO ADVANCING
           ACCEPT USER-NAME
           DISPLAY "Age? " WITH NO ADVANCING
           ACCEPT AGE
           COMPUTE NEXT-AGE = AGE + 1
           DISPLAY "Hi " FUNCTION TRIM(USER-NAME)
                   ", next year you are " NEXT-AGE

(with 01 NEXT-AGE PIC 99. declared as well). ACCEPT stores the typed line in the item using the usual MOVE rules: text is left-justified and space padded, and long input is cut off. For numeric receiving items, input that is not a number gives unreliable results, so validate it (see IF and conditions) before trusting it. Note that DISPLAY operands cannot be expressions, which is why the addition is done first with COMPUTE.

ACCEPT from the system

ACCEPT item FROM source reads values that the computer knows:

Statement Result
ACCEPT D FROM DATE YYMMDD, e.g. 260115 (6 digits)
ACCEPT D FROM DATE YYYYMMDD 20260115 (8 digits)
ACCEPT D FROM DAY YYDDD (year and day of year)
ACCEPT D FROM DAY YYYYDDD 2026015
ACCEPT D FROM DAY-OF-WEEK 1 digit, 1 = Monday ... 7 = Sunday
ACCEPT T FROM TIME HHMMSSss (8 digits, hundredths of a second)
COBOL
       01 TODAY.
          05 TODAY-YEAR    PIC 9(4).
          05 TODAY-MONTH   PIC 9(2).
          05 TODAY-DAY     PIC 9(2).
       01 NOW.
          05 NOW-HOUR      PIC 9(2).
          05 NOW-MIN       PIC 9(2).
          05 NOW-SEC       PIC 9(2).
          05 NOW-HUND      PIC 9(2).
       ...
           ACCEPT TODAY FROM DATE YYYYMMDD
           ACCEPT NOW FROM TIME
           DISPLAY "Run on " TODAY-DAY "/" TODAY-MONTH "/" TODAY-YEAR
                   " at " NOW-HOUR ":" NOW-MIN

Group items let you split the digits into parts, as the REDEFINES lesson showed. The two-digit year in plain FROM DATE caused the "Year 2000 problem", so prefer YYYYMMDD or FUNCTION CURRENT-DATE (see reference modification and intrinsic functions).

Arguments and environment (GnuCOBOL)

COBOL
           ACCEPT ARG-COUNT FROM ARGUMENT-NUMBER
           ACCEPT FIRST-ARG FROM ARGUMENT-VALUE
           ACCEPT HOME-DIR  FROM ENVIRONMENT "HOME"

On IBM mainframes, programs receive parameters through PARM in JCL and the LINKAGE SECTION instead.

Stand-in: splitting the date and time

The blocks here are Python stand-ins. They use a fixed date so the output is the same every time and show how a group item divides the digit string.

Python ACCEPT FROM DATE and TIME (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Stand-in: how DISPLAY joins operands and prints numbers

Python DISPLAY with several operands

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Common mistakes

  • Expecting DISPLAY to add spaces between operands.
  • Expecting a decimal point from a V field.
  • Using ACCEPT for a number and not checking it with IS NUMERIC afterward.
  • Using the two-digit-year DATE form in new code.
  • Forgetting WITH NO ADVANCING on a prompt, so the answer starts on a new line.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does DISPLAY "A" "B" print?

    Operands are written one after another with no separator.
  2. 2. Which statement reads today's date as eight digits (YYYYMMDD)?

    Plain FROM DATE gives six digits with a two-digit year. YYYYMMDD gives the full year.
  3. 3. What does WITH NO ADVANCING do?

    It leaves the cursor on the same line, which suits prompts.
  4. 4. ACCEPT TODAY-DAY FROM DAY-OF-WEEK returns 3. Which day is it?

    Monday is 1, so 3 is Wednesday.

Summary

  • DISPLAY joins its operands without separators; WITH NO ADVANCING keeps the cursor on the line; UPON SYSERR writes to the error stream.
  • ACCEPT reads a line into an item; validate numeric input afterwards.
  • ACCEPT ... FROM DATE [YYYYMMDD], DAY, DAY-OF-WEEK and TIME read system values; group items split them into parts.
  • Numeric items display as raw digits; use edited pictures for readable output.

Next: IF, ELSE and Conditions