COBOL-从年龄中查找出生年份

COBOL- Find year of Birth from age

我使用以下代码使用 cobol 仅从年龄中查找出生年份,但无法将年份转换为数字变量。我的密码是

IDENTIFICATION DIVISION.
PROGRAM-ID. AGECONV.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 CURR-YEAR PIC X(4) VALUES SPACES.
01 YEAR-OF-BIRTH X(4).
PROCEDURE DIVISION
MOVE FUNCTION CURRENT-DATE TO CURR-YEAR.
DISPLAY CURR-YEAR
SUBTRACT CURR-YEAR BY 22
GIVING YEAR-OF-BIRTH.
DISPLAY YEAR-OF-BIRTH.

我得到的错误是 curr-year 不是数字。

您需要更精细的工作存储部分。

IDENTIFICATION DIVISION.
PROGRAM-ID. AGECONV.

DATA DIVISION.
WORKING-STORAGE SECTION.
01  WS-CURRENT-DATE-DATA.
    05  WS-CURRENT-DATE.
        10  WS-CURRENT-YEAR         PIC 9(04).
        10  WS-CURRENT-MONTH        PIC 9(02).
        10  WS-CURRENT-DAY          PIC 9(02).
    05  WS-CURRENT-TIME.
        10  WS-CURRENT-HOURS        PIC 9(02).
        10  WS-CURRENT-MINUTE       PIC 9(02).
        10  WS-CURRENT-SECOND       PIC 9(02).
        10  WS-CURRENT-MILLISECONDS PIC 9(02).

01  YEAR-OF-BIRTH                   PIC 9(04).

PROCEDURE DIVISION.
MOVE FUNCTION CURRENT-DATE TO WS-CURRENT-DATE-DATA
DISPLAY WS-CURRENT-YEAR
SUBTRACT WS-CURRENT-YEAR BY 22 GIVING YEAR-OF-BIRTH
DISPLAY YEAR-OF-BIRTH.

对于 SUBTRACT,发送字段 CURR-YEAR 需要为数字。它被定义为字母数字。这意味着 CURR-YEAR 需要 PIC 9(4)。缺少 PIC.

YEAR-OF-BIRTH 也是如此

FUNCTION CURRENT-DATE 是字母数字发送字段。 MOVE 的规则不允许将其直接移动到数字字段。添加了引用修饰符 (1:4) 以允许移动。

SUBTRACT 语句的格式不正确,已更改。

修改后的代码:

   IDENTIFICATION DIVISION.
   PROGRAM-ID. AGECONV.
   DATA DIVISION.
   WORKING-STORAGE SECTION.
   01 CURR-YEAR PIC 9(4) VALUE ZERO.
   01 YEAR-OF-BIRTH PIC 9(4).
   PROCEDURE DIVISION.
   MOVE FUNCTION CURRENT-DATE (1:4) TO CURR-YEAR.
   DISPLAY CURR-YEAR
   SUBTRACT 22 FROM CURR-YEAR
   GIVING YEAR-OF-BIRTH.
   DISPLAY YEAR-OF-BIRTH.

输出:

2020
1998