Command Wait in COBOL?

Viewed 1613

Is there a kind of "Wait" function in COBOL? I wrote a calculator, and to make it more 50s, i Print " Computing." "Computing.." ecc For example:

                   DISPLAY "SECONDO NUMERO"
                   ACCEPT B
                   COMPUTE C= A * B
                          DISPLAY "Computing"
                          DISPLAY "Computing."
                          DISPLAY "Computing.."
                          DISPLAY "Computing..."
                          DISPLAY "Computing...."
                          DISPLAY "Computing....."
                          DISPLAY "Computing......"
                          DISPLAY A "x" B " FA..."
                          DISPLAY C

Now, is there a way to make a little delay (half a second) on COBOL where I put the "Computing" piece? I created a github repo (https://github.com/aIDserse/Super-utility-Submachine-COBOL-CALCULATOR) to this project, look at it (refer to version 1.3) for the complete code (and maybye spread it hahah). Thx!!!

4 Answers

There is a statement for sleeping in standard COBOL, but only with COBOL 202x:

           CONTINUE AFTER arithmetic-expression SECONDS

As this standard is in the committee draft state it is hard to find an implementation, but as you've asked for GnuCOBOL - GnuCOBOL 3.1 already implements it.

Other than this there are some dialect specific library routines that can be used, like CALL "C$SLEEP" originating from ACUCOBOL-GT (also implemented with GnuCOBOL, but be aware that pre 3.1-versions only use the non-decimal part, so "0.9" will sleep zero seconds).

For OpenCOBOL/GnuCOBOL you can call the CBL_OC_NANOSLEEP/CBL_GC_NANOSLEEP library routines.

For any COBOL environment that can call native routines you have variants of CALL "sleep".

As mentioned by Rick Smith Many COBOL implementations also implement a callable SYSTEM where you may use something like a ping localhost with a timeout, but whatever you call may not be available (or the process running the COBOL environment has no access to it).

Stephen Gennard mentioned a very common extension:

           ACCEPT something WITH TIMEOUT

which has a "beware" that different environments use a different scale (some seconds, some milliseconds). This has the pro/con that the user can "break" out by pressing a key (normally a function key); and the additional issue that it may only work in "graphical" environments.

Anton's answer highlights the IBM library routine CEE3DLY.

There's no wait statement in any ISO Standard COBOL.

However, if you got built in system routines available either C$SLEEP (for seconds) or CBL_GC_NANOSLEEP (for nanoseconds) should do the trick.

Example (sleeps for half a second):

call "CBL_GC_NANOSLEEP" using "500000000" end-call

For IBM's Enterprise COBOL (LE enabled) the CEE3DLY routine is most suitable (there are also other legacy routines available).

There is no wait or delay statement in standard COBOL. There may be, for GnuCOBOL, a CALL "SYSTEM" to effect a delay.

I took some code that I use for elapsed time measurement and modified the code to create a procedure for a delay.

Wherever you need a delay, insert the statement PERFORM timed-delay. Of course, the delay may be changed. This code is set to work even if the delay crosses midnight.

Code:

   working-storage section.
   01 t pic 9(8).
   01 t-start.
     03 t-start-hour pic 99.
     03 t-start-minute pic 99.
     03 t-start-second pic 99v99.
   01 t-end.
     03 t-end-hour pic 99.
     03 t-end-minute pic 99.
     03 t-end-second pic 99v99.
   77 t-elapsed pic 9(7)v99.
   procedure division.
   begin.
       accept t from time
       display t
       perform timed-delay
       accept t from time
       display t
       stop run
       .

   timed-delay.
       accept t-start from time
       move 0 to t-elapsed
       perform until t-elapsed > 0.5   *> one-half second
           accept t-end from time
           perform get-elapsed
       end-perform
       .

   get-elapsed.
       if t-start > t-end
           move 86400 to t-elapsed
       else
           move 0 to t-elapsed
       end-if
       compute t-elapsed = t-elapsed
         + (t-end-hour - t-start-hour) * 3600
         + (t-end-minute - t-start-minute) * 60
         + (t-end-second - t-start-second)
       end-compute
       .

Output: (shows a delay of 0.55 seconds)

21424364
21424419

The initial PERFORM WITH TEST AFTER ... is nothing like the code I provided in: Cobol-Restart from the program , so I turned it into comments. It should be removed.

If you want to use SLEEP-SEC instead of a fixed value, replace the 0.5 with SLEEP-SEC; but provide a VALUE clause for SLEEP-SEC or MOVE a value to it before the displaying the menu.

For example, in your code (with most code removed):

   DATA DIVISION.
   WORKING-STORAGE SECTION.
   01  SLEEP-SEC PIC S9(2)V9(2).
   01  A PIC S9(7)V9(7).
   01  B PIC S9(7)V9(7).
   01  C PIC S9(7)V9(7).
   01  D PIC S9(11)V9(7).
   01  INPUT1 PIC 9(14).
   01  Q PIC X VALUE "Y".
   01 t-start.
     03 t-start-hour pic 99.
     03 t-start-minute pic 99.
     03 t-start-second pic 99v99.
   01 t-end.
     03 t-end-hour pic 99.
     03 t-end-minute pic 99.
     03 t-end-second pic 99v99.
   77 t-elapsed pic 9(7)v99.

       PROCEDURE DIVISION.
   MAIN.
  *    PERFORM WITH TEST AFTER
  *        UNTIL Q ="YES" OR "Y" OR "y" OR "yes" OR "Yes"
  *    END-PERFORM.
       DISPLAY "CALCULATOR".
       DISPLAY "WHAT DO YOU WANT DO DO?".
       DISPLAY "1 ADDITION".
       DISPLAY "15 EXIT"
       DISPLAY "CHOOSE AN OPTION"
       ACCEPT INPUT1
       EVALUATE  INPUT1

       WHEN = 15
            DISPLAY "OK, GOOD JOB :)"
            STOP RUN

       WHEN = 1
       DISPLAY "FIRST NUMBER"
       ACCEPT A
       DISPLAY "SECOND NUMBER"
       ACCEPT B
       COMPUTE C= A + B
               DISPLAY "Computing"
       PERFORM timed-delay
               DISPLAY "(" A ")" "+" "(" B ")" "RESULTS..."
               DISPLAY C

       END-EVALUATE

               IF INPUT1 NOT = 15
           DISPLAY "DO YOU WANT TO DO OTHER CALCULATIONS?"
           ACCEPT Q
           IF Q = "YES" OR "Y" OR "y" OR "yes" OR "Yes" GO TO MAIN
               ELSE DISPLAY "OK, GOOD JOB :)"
              END-IF
              STOP RUN.

   timed-delay.
       accept t-start from time
       move 0 to t-elapsed
       perform until t-elapsed > 0.5   *> one-half second
           accept t-end from time
           perform get-elapsed
       end-perform
       .

   get-elapsed.
       if t-start > t-end
           move 86400 to t-elapsed
       else
           move 0 to t-elapsed
       end-if
       compute t-elapsed = t-elapsed
         + (t-end-hour - t-start-hour) * 3600
         + (t-end-minute - t-start-minute) * 60
         + (t-end-second - t-start-second)
       end-compute
       .

For GnuCobol call the C$SLEEP with the number of seconds you want to wait.

CALL "C$SLEEP" USING 2 END-CALL

COBOL has no build in language feature to handle waiting. This is a system specific request and I believe always requires calling an external module to interface with said system.

Related