Two optional parts of the data division describe output by layout instead of by statements: the report section (Report Writer, COBOL-74 on) for printed reports, and the screen section (2002; Micro Focus, GnuCOBOL and ACUCOBOL earlier) for text terminals. Both were made optional in the 2014 standard, and IBM Enterprise COBOL supports neither. GnuCOBOL, Micro Focus and Fujitsu do.
Report Writer
A program names the report on its file’s FD (REPORT IS), describes the layout in the REPORT SECTION,
and drives it with three statements. The runtime does the page, line and control-break counting.
FILE SECTION.
FD PRINT-FILE REPORT IS SALES-REPORT.
REPORT SECTION.
RD SALES-REPORT
CONTROLS ARE FINAL REGION
PAGE LIMIT IS 60 LINES
HEADING 1 FIRST DETAIL 5 LAST DETAIL 56 FOOTING 58.
01 TYPE IS PAGE HEADING.
05 LINE 1 COLUMN 30 PIC X(12) VALUE "SALES REPORT".
05 LINE 3 COLUMN 1 PIC X(6) VALUE "REGION".
05 COLUMN 20 PIC X(6) VALUE "AMOUNT".
01 SALE-LINE TYPE IS DETAIL.
05 LINE PLUS 1 COLUMN 1 PIC X(10) SOURCE REGION.
05 COLUMN 20 PIC ZZZ,ZZ9.99 SOURCE AMOUNT.
01 TYPE IS CONTROL FOOTING REGION.
05 LINE PLUS 2 COLUMN 1 PIC X(7) VALUE "SUBTOTAL".
05 COLUMN 20 PIC ZZZ,ZZ9.99 SUM AMOUNT.
01 TYPE IS CONTROL FOOTING FINAL.
05 LINE PLUS 2 COLUMN 1 PIC X(5) VALUE "TOTAL".
05 COLUMN 20 PIC Z,ZZZ,ZZ9.99 SUM AMOUNT.
PROCEDURE DIVISION.
OPEN OUTPUT PRINT-FILE
INITIATE SALES-REPORT
PERFORM UNTIL NO-MORE
GENERATE SALE-LINE
END-PERFORM
TERMINATE SALES-REPORT
CLOSE PRINT-FILE.
| Element | Meaning |
|---|---|
RD name with CODE, CONTROL[S] IS|ARE FINAL a b…, PAGE LIMIT[S], HEADING, FIRST DETAIL, LAST DETAIL, FOOTING | Describes a report: the control fields (major to minor, FINAL = the whole report) and the page layout. |
TYPE IS REPORT HEADING (RH), PAGE HEADING (PH), CONTROL HEADING (CH), DETAIL (DE), CONTROL FOOTING (CF), PAGE FOOTING (PF), REPORT FOOTING (RF) | The seven kinds of report group, named by the position they print in. |
LINE [NUMBER] IS n | PLUS n | NEXT PAGE, COLUMN [NUMBER] IS n | Where an item prints: absolute or relative line; column. |
SOURCE IS x | The value printed, taken from a data item when the line is generated. |
VALUE IS lit | A constant. |
SUM x [UPON detail] [RESET ON control] | A total accumulated for each GENERATE, printed at a control footing, reset at a break. The RESET ON FINAL / a control name; SUM OF sums a lower total. |
GROUP INDICATE | Print the item only on the first detail after a heading or page break. |
NEXT GROUP IS | Space after a group. |
PRESENT WHEN cond (2002) | Print the item only if the condition holds. |
PIC, JUST, BLANK WHEN ZERO, USAGE, OCCURS (2002), COLUMN with several values | As in the data division. |
INITIATE rpt | Opens the report: counters to zero, first headings pending. |
GENERATE name | Prints a detail group, or with a report name, a summary (totals only); the control break tests run first. |
TERMINATE rpt | Finishes the report: last control footings and report footing. |
SUPPRESS PRINTING | In a USE BEFORE REPORTING declarative: skip printing this group. |
PAGE-COUNTER, LINE-COUNTER | Special registers of each report; usable as SOURCE. |
Control breaks are tested by comparing the control fields at each GENERATE: when a field changes, the matching control
footings (minor to major) and control headings (major to minor) are printed first. The GnuCOBOL Programmer’s Guide has a long chapter on it
(“RWCS”).
Screen section
SPECIAL-NAMES.
CRT STATUS IS KEY-STATUS
CURSOR IS CURSOR-POS.
SCREEN SECTION.
01 LOGIN-SCREEN.
05 BLANK SCREEN BACKGROUND-COLOR 1 FOREGROUND-COLOR 7.
05 LINE 3 COLUMN 10 VALUE "User name:".
05 USER-FLD LINE 3 COLUMN 22 PIC X(12)
USING WS-USER AUTO REQUIRED.
05 LINE 4 COLUMN 10 VALUE "Password:".
05 PASS-FLD LINE 4 COLUMN 22 PIC X(12)
TO WS-PASS SECURE.
05 LINE 6 COLUMN 10 VALUE "F3 = quit" HIGHLIGHT.
PROCEDURE DIVISION.
DISPLAY LOGIN-SCREEN
ACCEPT LOGIN-SCREEN
| Clause | Meaning |
|---|---|
LINE [NUMBER] [IS] [PLUS] n, COLUMN / COL (also PLUS) | Position; relative to the item before. |
BLANK SCREEN, BLANK LINE, ERASE EOL, ERASE EOS | Clear before display. |
FOREGROUND-COLOR n, BACKGROUND-COLOR n | Colour 0 to 7 (black, blue, green, cyan, red, magenta, brown or yellow, white); GnuCOBOL also takes FOREGROUND-COLOUR. |
HIGHLIGHT, LOWLIGHT, REVERSE-VIDEO, UNDERLINE, BLINK, BELL (BEEP) | Attributes. |
VALUE "text" | Constant text. |
FROM x, TO x, USING x | Display only; accept into; both (display, then accept back). |
PIC, BLANK WHEN ZERO, JUSTIFIED, SIGN, SIZE | Field shape, as in data entries. |
AUTO (AUTO-SKIP), SECURE, REQUIRED, FULL, PROMPT [CHARACTER IS c], UPDATE, NO-ECHO | Entry behaviour: move on when full; hide typing; must be filled; must be filled completely; prompt fill character; show current value. |
CRT STATUS IS item (in SPECIAL-NAMES) | An item that gets the key that ended ACCEPT (function keys, escape, timeout). GnuCOBOL: COB-CRT-STATUS. |
CURSOR IS item | Where the cursor is on return. |
Without a screen section, DISPLAY ... AT LINE n COLUMN m and ACCEPT ... AT put single items at a screen position
in the compilers that support it, and the GnuCOBOL and Micro Focus libraries add calls for cursor, keyboard and window control.
Fujitsu NetCOBOL, ACUCOBOL-GT and RM/COBOL have richer screen and window systems of their own, which these pages do not describe.