COBOL Tutorial

Progress0 of 0 lessons

COBOL User Interface

The user interface in COBOL is how your program talks to the user: showing messages and prompts and reading what they type. Most COBOL programs use a character-based interface built from two main verbs—DISPLAY and ACCEPT—and, in some compilers, a Screen Section that defines full-screen layouts. This page explains these building blocks and how to use them for simple, clear interaction.

Explain Like I'm Five: What Is the User Interface?

The user interface is what the user sees and uses. When the program shows a message like "Enter your name:" and waits for the user to type something, that is the user interface. COBOL does this with DISPLAY (to show text) and ACCEPT (to read what was typed). Think of DISPLAY as the program speaking and ACCEPT as the program listening.

DISPLAY and ACCEPT: The Core Verbs

DISPLAY sends data to the screen (or another device). You can display literals, variables, or both. ACCEPT reads input from the keyboard (or from the system, e.g. date or time) and stores it in a variable. Together they form the basis of a simple conversation with the user.

Core user interface verbs
VerbPurposeExample
DISPLAYSend data to screen or deviceDISPLAY "Enter ID: ".
ACCEPTRead input from keyboard or systemACCEPT WS-USER-ID.

Simple DISPLAY and ACCEPT Example

In this snippet, the program prompts for a customer number, reads it into WS-CUST-NUM, and then displays it back. The DISPLAY lines show text; the ACCEPT line reads the value into the variable.

cobol
1
2
3
DISPLAY "Enter customer number: " ACCEPT WS-CUST-NUM DISPLAY "You entered: " WS-CUST-NUM

DISPLAY in More Detail

DISPLAY can output one or more items. Each item can be a literal (in quotes), a data name (variable), or a combination. Items are sent in order; spacing depends on how you separate them. Some implementations support WITH NO ADVANCING so the cursor stays on the same line after the DISPLAY, which is useful for prompts where the user types on the same line.

cobol
1
2
3
4
5
6
7
*> Cursor moves to next line after each DISPLAY DISPLAY "Name: " DISPLAY WS-NAME *> With NO ADVANCING, prompt and input can be on same line DISPLAY "Name: " WITH NO ADVANCING ACCEPT WS-NAME

ACCEPT in More Detail

ACCEPT reads input into a variable. Without any extra clauses, it typically reads from the keyboard. You can also use ACCEPT ... FROM to get system values, for example ACCEPT WS-DATE FROM DATE or ACCEPT WS-TIME FROM TIME. The receiving field must be large enough and have a type (e.g. numeric or alphanumeric) that matches how you use the data.

Screen Section (When Available)

Some COBOL compilers support a Screen Section in the Data Division. There you define a screen by giving each element a line and column position, and whether it is output-only, input, or both. You then DISPLAY the screen to show it and ACCEPT the screen to read user input. This gives you a full-screen form instead of line-by-line DISPLAY and ACCEPT. Support and syntax vary by vendor (e.g. Micro Focus, IBM).

Step-by-Step: A Simple Prompt and Input

  • Reserve a variable in Working-Storage for the input (e.g. 05 WS-INPUT PIC X(20)).
  • Use DISPLAY to show a clear prompt (e.g. "Enter value: ").
  • Use ACCEPT to read the user's input into that variable.
  • Validate the input if needed (range, format, etc.) and DISPLAY success or error messages.

Test Your Knowledge

1. Which COBOL verb sends data to the screen?

  • ACCEPT
  • DISPLAY
  • READ
  • WRITE

2. Which verb reads input from the user into a variable?

  • DISPLAY
  • ACCEPT
  • MOVE
  • WRITE

Related Concepts

Related Pages