Search code examples
cobolgnucobol

Accepting signed values as part of a record


I have a fairly basic Cobol program I'm using to learn about record structures. I'm noticing strange behaviour with gnucobol when passing signed numeric values as part of the record in an ACCEPT statement.

The program is defined as follows:

IDENTIFICATION DIVISION.
PROGRAM-ID. TEST.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 Account.
  02 Name PIC X(5) VALUE SPACES.
  02 Balance PIC S9999V999 VALUE ZEROES.
PROCEDURE DIVISION.
MAIN.
  DISPLAY "Enter account details:"
  ACCEPT Account.
  DISPLAY "Balance is:"
  DISPLAY Balance.

  STOP RUN.

The behaviour is as follows:

Enter account details:
AAAAA-123.456
Balance is:
+-123.045

I assume this is due to how the value is stored in raw memory.

Is this generally what most cobol compilers do? Is there a way to get cobol interpreting the signed value properly?


Solution

  • I assume this is due to how the value is stored in raw memory.

    yes

    Is this generally what most cobol compilers do?

    a guess: yes (the actual display will vary, but I'm sure most COBOL environments won't do what you seem to want them, at least this way)

    Is there a way to get cobol interpreting the signed value properly?

    Yes, but there are "COBOL" things to do:

    • store data in internal format (like you did: signed value with implied 3 decimal positions), but for ACCEPT and likely also DISPLAY use a format that actually has the data as you want, for example a PIC +ZZZ9.999$, for some details look at this answer
    • never ACCEPT a record, either split into multiple ACCEPT or use a single one with accepting a screen-name, not a record name --> use the SCREEN SECTION for entering the data, this will provide you with two separate fields and with most COBOL environments reasonable input validation.