我使用以下代码使用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.
我得到的错误是本年度不是数字。
您需要一个更详细的工作-存储部分。
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
我是这样做的,传递了一个活动来在我的组中显示这些附加信息
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.
SUBTRACT 1999 FROM CURR-YEAR
GIVING YEAR-OF-BIRTH.
DISPLAY 'Me chamo Thiago Lyra e tenho ' YEAR-OF-BIRTH ' anos de idade'.
DISPLAY 'Meu email é: thiago.lyra@globality.com'.