Chapter 5: Key Takeaways -- Basic Input/Output and DISPLAY/ACCEPT Statements

Chapter Summary

Chapter 5 covers the fundamental building blocks of COBOL input and output: the DISPLAY and ACCEPT statements. While deceptively simple on the surface, these two statements support a rich set of capabilities that every COBOL programmer uses daily. DISPLAY sends output to the console or system output device, concatenating multiple items on a single line, supporting the WITH NO ADVANCING clause for same-line prompts, and routing output to specific devices with the UPON clause. ACCEPT retrieves input from the user or from the system, providing access not only to keyboard input but also to system date (in multiple formats), time, day of week, Julian date, environment variables, and command-line arguments.

The chapter emphasizes the critical difference between raw and formatted output. Raw DISPLAY of PIC 9 fields shows leading zeros; raw DISPLAY of PIC X fields includes trailing spaces. Professional COBOL programs transform data for human consumption using edited PICTURE clauses (Z for zero suppression, $ for currency, commas, and decimal points) and structured record lines in WORKING-STORAGE where FILLER items provide spacing between columns. The standard pattern -- define an output record structure, MOVE values into sub-fields, DISPLAY the entire record -- is the foundation of all COBOL report generation, whether output goes to a console, print file, or spool dataset.

The chapter also introduces the SCREEN SECTION (COBOL-2002 / GnuCOBOL) for building rich terminal interfaces with cursor positioning, colors, and field-level attributes, and contrasts interactive program patterns with batch program conventions. Interactive programs follow the menu-accept-process loop using PERFORM UNTIL and EVALUATE, with robust input validation that accepts into PIC X fields, validates with IS NUMERIC, and converts before use. The chapter concludes with important distinctions between STOP RUN (terminates the entire run unit) and GOBACK (returns to the caller), and between batch I/O patterns (file-driven, no user interaction) and interactive patterns (keyboard-driven, menu loops).

Key Concepts

  • DISPLAY concatenates multiple items (literals, variables, figurative constants) on a single output line with no automatic spacing between them. Trailing spaces from PIC X fields appear in the output.
  • WITH NO ADVANCING suppresses the newline after DISPLAY, keeping the cursor on the same line. Essential for creating prompts where user input appears on the same line as the prompt text.
  • DISPLAY UPON CONSOLE sends messages to the system operator console on IBM mainframes (WTO messages). DISPLAY UPON SYSOUT sends to the SYSOUT DD statement. In GnuCOBOL, both route to stdout.
  • ACCEPT reads a line of input from the user. Input into PIC X fields is left-justified with trailing spaces. Input into PIC 9 fields is right-justified with leading zeros.
  • ACCEPT FROM DATE YYYYMMDD retrieves the system date as an 8-digit number (YYYYMMDD). Always use the four-digit year format -- the two-digit DATE format is a Y2K-era relic.
  • ACCEPT FROM TIME returns an 8-digit value in HHMMSSss format (hours, minutes, seconds, hundredths of a second) using 24-hour time.
  • ACCEPT FROM DAY YYYYDDD returns the Julian date. ACCEPT FROM DAY-OF-WEEK returns a single digit (1=Monday through 7=Sunday, ISO 8601).
  • ACCEPT FROM ENVIRONMENT-VALUE reads operating system environment variables after setting the variable name with DISPLAY UPON ENVIRONMENT-NAME.
  • Edited PICTURE clauses transform numeric data for display: Z suppresses leading zeros, $ adds currency signs, commas insert thousands separators, and period inserts the decimal point.
  • Structured record lines in WORKING-STORAGE (with FILLER for spacing) produce perfectly aligned columnar output. The pattern is: define the record, MOVE values to sub-fields, DISPLAY the record.
  • The STRING statement concatenates fields with trimming via DELIMITED BY SPACES, which stops at the first space and effectively removes trailing padding.
  • The SCREEN SECTION (COBOL-2002 / GnuCOBOL only, not IBM Enterprise COBOL) defines screen layouts with LINE/COLUMN positioning, colors (FOREGROUND-COLOR, BACKGROUND-COLOR), visual attributes (HIGHLIGHT, UNDERLINE, REVERSE-VIDEO, SECURE), and field binding (USING, FROM, TO).
  • Input validation best practice: accept into a PIC X field, test with IS NUMERIC, then MOVE to a PIC 9 field. This prevents data exceptions from non-numeric input.
  • STOP RUN terminates the entire run unit (all programs in the call chain). GOBACK returns to the caller or to the OS if this is the main program. Prefer GOBACK as the default.

Common Pitfalls

  • Trailing spaces in concatenated DISPLAY output. When displaying WS-FIRST WS-LAST where both are PIC X(15), the trailing spaces in WS-FIRST create a large gap. Use STRING with DELIMITED BY SPACES to trim, or use FUNCTION TRIM if available.
  • Accepting numeric input directly into PIC 9 fields. If the user enters non-numeric characters, the field contains invalid data and any subsequent arithmetic causes a data exception (abend). Always accept into PIC X, validate, then convert.
  • Using ACCEPT FROM DATE instead of ACCEPT FROM DATE YYYYMMDD. The two-digit year format is ambiguous (is 26 the year 2026 or 1926?) and was a central cause of the Y2K crisis. Always use the four-digit year form.
  • Forgetting WITH NO ADVANCING on prompts. Without it, the user's input appears on the line below the prompt text, producing an awkward interface.
  • Displaying raw numeric fields without editing. PIC 9(7)V99 containing 125000.50 displays as "012500050" -- meaningless to users. Always MOVE to an edited field before displaying financial data.
  • Using DISPLAY/ACCEPT for user I/O in CICS programs. IBM CICS programs must use BMS maps (EXEC CICS SEND MAP / RECEIVE MAP) for terminal communication. DISPLAY and ACCEPT are for batch and TSO environments only.
  • Assuming SCREEN SECTION is portable. The SCREEN SECTION works in GnuCOBOL but is not supported by IBM Enterprise COBOL. Programs that use it cannot be directly ported to mainframe environments.
  • Not validating menu choices in interactive programs. Without validation loops, invalid input causes unexpected EVALUATE/WHEN OTHER processing or, worse, falls through without any handling.

Quick Reference

DISPLAY Statement:
  DISPLAY "literal" identifier figurative-constant ...
      [UPON {CONSOLE | SYSOUT}]
      [WITH NO ADVANCING]

ACCEPT Statement:
  ACCEPT identifier
      [FROM {CONSOLE | DATE [YYYYMMDD] | TIME |
             DAY [YYYYDDD] | DAY-OF-WEEK |
             ENVIRONMENT-VALUE | COMMAND-LINE}]

System Information:
  FROM DATE           PIC 9(6)  YYMMDD       (avoid -- Y2K risk)
  FROM DATE YYYYMMDD  PIC 9(8)  YYYYMMDD     (always use this)
  FROM TIME           PIC 9(8)  HHMMSSss     (24-hour + hundredths)
  FROM DAY YYYYDDD    PIC 9(7)  YYYYDDD      (Julian date)
  FROM DAY-OF-WEEK    PIC 9     1=Mon 7=Sun  (ISO 8601)

Formatted Output Pattern:
  01  WS-DETAIL-LINE.
      05  FILLER        PIC X(2)  VALUE SPACES.
      05  WS-DET-ID     PIC 9(6).
      05  FILLER        PIC X(3)  VALUE SPACES.
      05  WS-DET-NAME   PIC X(25).
      05  FILLER        PIC X(3)  VALUE SPACES.
      05  WS-DET-AMT    PIC $$$,$$$,$$9.99.

  MOVE WS-ID     TO WS-DET-ID
  MOVE WS-NAME   TO WS-DET-NAME
  MOVE WS-AMOUNT TO WS-DET-AMT
  DISPLAY WS-DETAIL-LINE

Input Validation Pattern:
  01  WS-INPUT  PIC X(10).
  01  WS-AMOUNT PIC 9(7)V99.

  DISPLAY "Enter amount: " WITH NO ADVANCING
  ACCEPT WS-INPUT
  IF WS-INPUT IS NUMERIC
      MOVE WS-INPUT TO WS-AMOUNT
  ELSE
      DISPLAY "Invalid input."
  END-IF

Interactive Menu Pattern:
  PERFORM UNTIL EXIT-REQUESTED
      PERFORM SHOW-MENU
      DISPLAY "Choice: " WITH NO ADVANCING
      ACCEPT WS-CHOICE
      EVALUATE WS-CHOICE
          WHEN "1"  PERFORM OPTION-ONE
          WHEN "0"  SET EXIT-REQUESTED TO TRUE
          WHEN OTHER DISPLAY "Invalid choice."
      END-EVALUATE
  END-PERFORM

SCREEN SECTION (GnuCOBOL):
  SCREEN SECTION.
  01  MY-SCREEN.
      05  BLANK SCREEN.
      05  LINE 1 COLUMN 1 VALUE "TITLE" HIGHLIGHT.
      05  LINE 3 COLUMN 5 VALUE "Name: ".
      05  LINE 3 COLUMN 11 PIC X(20) USING WS-NAME UNDERLINE.

  DISPLAY MY-SCREEN
  ACCEPT MY-SCREEN

What's Next

Chapter 6 covers COBOL's five arithmetic verbs -- ADD, SUBTRACT, MULTIPLY, DIVIDE, and COMPUTE -- along with the critical supporting features that make financial calculations reliable: the ROUNDED phrase for controlling rounding behavior, ON SIZE ERROR for detecting overflow and division by zero, the GIVING phrase for preserving operands, decimal alignment, and the COMP-3 packed-decimal format. Together with the I/O skills from this chapter, arithmetic operations will enable you to build programs that process and transform financial data with the precision that business demands.