Simple cobol "SCREEN" to display and get keyboard input.

This example below uses gnuCOBOL's idea of "SCREENS". Screens are method of displaying ncurses/curses style displays with labels and keyboard inputs (I read that mouse inputs may be possible, but not in this demo.)

This code may work on other versions of COBOL, but I have not tested. I'm going to use CAPS in my code, deal with it.

Below is the complete program that can be compiled at the shell with the command line:

cobc -xF simple-question.cob -o simple-question

And the cobol code:

 1: IDENTIFICATION DIVISION.
 2: PROGRAM-ID. SAMPLE.
 3: 
 4: ENVIRONMENT DIVISION.
 5: CONFIGURATION SECTION.
 6: SPECIAL-NAMES.
 7: 
 8: INPUT-OUTPUT SECTION.
 9: FILE-CONTROL.
10: 
11: DATA DIVISION.
12: FILE SECTION.
13: 
14: WORKING-STORAGE SECTION.
15: 01  RESPONSE.
16:   05  RESPONSE-IN-WS     PIC X(2).
17: 
18: SCREEN SECTION.
19: 01 SIMPLE-QUESTION-SCREEN.
20:    05  VALUE "SIMPLE QUESTION SCREEN" BLANK SCREEN       LINE 1 COL 35.
21:    05  VALUE "ANSWER YES OR NO!  Y/N: "                  LINE 2 COL 1.
22:    05  RESPONSE-INPUT                                    LINE 2 COL 25
23:        PIC X TO RESPONSE-IN-WS.
24: 
25: PROCEDURE DIVISION.
26:    DISPLAY SIMPLE-QUESTION-SCREEN.
27:    ACCEPT SIMPLE-QUESTION-SCREEN.
28:    STOP RUN.
29: 

Starting at the "meat" of the demonstration, the "SCREEN SECTION".

18: SCREEN SECTION.
19: 01 SIMPLE-QUESTION-SCREEN.
20:    05  VALUE "SIMPLE QUESTION SCREEN" BLANK SCREEN       LINE 1 COL 35.
21:    05  VALUE "ANSWER YES OR NO!  Y/N: "                  LINE 2 COL 1.
22:    05  RESPONSE-INPUT                                    LINE 2 COL 25
23:        PIC X TO RESPONSE-IN-WS.

The "SCREEN SECTION" is instructions for the "DISPLAY" function to layout and handle basic layout on the screen. Specific line and column need to be specified to ensure that the labels and input boxes are exactly as requested. (PROTIP: draw them out in a text editor with fixed width fonts before coding up a screen, it will save you time in more complex scenarios.)

25: PROCEDURE DIVISION.
26:    DISPLAY SIMPLE-QUESTION-SCREEN.
27:    ACCEPT SIMPLE-QUESTION-SCREEN

The "DISPLAY" function shows the screen. The "ACCEPT" function deals with the inputs from the user. In this case it is simply a Y/N answer and the result is stored in "RESPONSE-IN-WS".

It is useful to know that you may wish to create more complex interfaces where you could call multiple DISPLAYS, even though part of them may not be interactive at this stage in the program.

NB. I'm gradually improving my understanding of "State" diagrams that many cobol sites used to explain the function diagrams. These are not intuitive and must be understood to grow as a developer.

And the final running example:

QceI3Rvl.png

Resources: