Search code examples
cobolgnucobol

How to clear screen and set cursor position to the end of the screen in cobol


So i'm trying to make a form-like screen where the user inputs his data and saves it in a .txt file. I'm using OpenCobolIDE and i'm currently going through problems in the clearing screen process. i have a kind of form that i made in the console screen and when the user enters his data im refreshing it with the new values of the variables, but the cursor position is screwing me up because after i clear the screen it gets reseted to the beginning of the console screen, and i want it to go to the end of the text that i'm displaying after this process. My explanation may look confusing but i hope you get my point with the code:

IDENTIFICATION DIVISION.
PROGRAM-ID.PGM001.

ENVIRONMENT DIVISION.

DATA DIVISION.

WORKING-STORAGE SECTION.
01 WS-DATA
    02 WS-ID    PIC X(03) VALUE SPACES.
    02 WS-NAME  PIC A(15) VALUE SPACES.
    02 WS-PHONE PIC X(09) VALUE SPACES.
SCREEN SECTION.
01 CLEAR-SCREEN.
    02 BLANK SCREEN.
PROCEDURE DIVISION.
MENU.
    DISPLAY "ID........:" WS-ID.
    DISPLAY "NAME......:" WS-NAME.
    DISPLAY "PHONE.....:" WS-PHONE.
    DISPLAY "-----------".

    DISPLAY "ID:".
    ACCEPT WS-ID FROM CONSOLE.

    DISPLAY CLEAR-SCREEN.
    DISPLAY "ID........:" WS-ID.
    DISPLAY "NAME......:" WS-NAME.
    DISPLAY "PHONE.....:" WS-PHONE.
    DISPLAY "-----------".

    DISPLAY "NAME:".
    ACCEPT WS-NAME FROM CONSOLE.

    DISPLAY CLEAR-SCREEN.
    DISPLAY "ID........:" WS-ID.
    DISPLAY "NAME......:" WS-NAME.
    DISPLAY "PHONE.....:" WS-PHONE.
    DISPLAY "-----------".

    DISPLAY "PHONE:".
    ACCEPT WS-PHONE FROM CONSOLE.

    DISPLAY CLEAR-SCREEN.
    DISPLAY "ID........:" WS-ID.
    DISPLAY "NAME......:" WS-NAME.
    DISPLAY "PHONE.....:" WS-PHONE.
    DISPLAY "-----------".

    STOP RUN.
END-PROGRAM PGM001.

So you'll notice that i keep clearing the screen and displayin the form but as i do it the cursor goes to the beginning of the screen and i can't keep inputing the data. Can someone help me please? Is there any command in cobol that moves the cursor?


Solution

  • In COBOL, there are two types of DISPLAY statement: those for a device and those for a screen.

    In OpenCOBOL, device and screen DISPLAYs cannot be used at the same time; if you try to, you will find that there is no output from the device DISPLAYs after the first screen DISPLAY. This is what is happening your example: CLEAR-SCREEN is defined in the screen section, so DISPLAY CLEAR-SCREEN is a screen DISPLAY.

    You can fix this by defining the entry form in the screen section:

    SCREEN SECTION.
    01  form BLANK SCREEN.
        03  VALUE "ID.........".
        03  COL + 2, PIC X(03) TO WS-ID.
        03  LINE + 1, VALUE "NAME.......".
        03  COL + 2, PIC A(15) TO WS-NAME.
        03  LINE + 1, VALUE "PHONE......".
        03  COL + 2, PIC X(09) TO WS-PHONE.
        03  LINE + 1, VALUE "-----------".
    

    As well as working, this has the extra advantage that the procedure division can be reduced to

     DISPLAY form
     ACCEPT form
    

    as all the form data can be entered in one go.

    If, however, you want to keep data entry as it is, you can turn the device DISPLAYs into screen DISPLAYs by adding AT LINE <line-num>:

    DISPLAY "ID........:" WS-ID AT LINE 1
    DISPLAY "NAME......:" WS-NAME AT LINE 2
    DISPLAY "PHONE.....:" WS-PHONE AT LINE 3
    DISPLAY "-----------" AT LINE 4
    
    DISPLAY "ID:" AT LINE 5
    ACCEPT WS-ID AT LINE 5, COL 5