0

正如标题所示,我很难为我的 COBOL 程序获得所需的输出。该程序从文件中获取数据并以特定格式输出,这是用于家庭作业的。当我运行程序时,它适用于名称标题、schoolID 标题和列标题。第一个课程效果很好,然后输出开始减少一个并弄乱我所有的其他输出。

输入文件示例

CMPS161 ALGORITHM DSGN/IMPLMNT I A 3.00 CMPS280 ALGORITHM DSGN/IMPLEM II B 3.00

输出文件所需示例

<Name> <schoolID> Course Title GR Earned CMPS161 ALGORITHM DSGN/IMPLMNT I A 3.00 CMPS280 ALGORITHM DSGN/IMPLEM II B 3.00

输出实际示例

<Name> <schoolID> Course Title GR Earned CMPS161 ALGORITHM DSGN/IMPLMNT I A 0.00 MPS280 ALGORITHM DSGN/IMPLEM II B 0.00

COBOL 项目

IDENTIFICATION DIVISION.
PROGRAM-ID.      P2.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
        SELECT myInFile ASSIGN TO "P2In.dat".
        SELECT myOutFile ASSIGN TO "P2Out.dat".
DATA DIVISION.
FILE SECTION.
FD myInFile.
01 inRecord.
   02 Course   PIC X(13).
   02 Title    PIC X(32).
   02 Grade    PIC X(4).
   02 Earned   PIC Z9.99.
   02 FILLER   PIC X(3).
FD myOutFile.
01 outRecord.
   02 myCourse  PIC X(13).
   02 myTitle   PIC X(32).
   02 myGrade   PIC X(4).
   02 myEarned  PIC Z9.99.
   02 FILLER    PIC X(3).
WORKING-STORAGE SECTION.
01 E0F PIC X(3) VALUE "NO ".
01 NAME-HDR.
    05 FILLER    PIC X(10) VALUE "NAME HERE ".
01 SCHOOLID-HDR.
    05 FILLER    PIC X(8) VALUE "SCHOOLID"
01 COLUMN-HDR.
    05 FILLER   PIC X(6)  VALUE "COURSE".
    05 FILLER   PIC X(6)  VALUE  SPACES.
    05 FILLER   PIC X(6)  VALUE "TITLE".
    05 FILLER   PIC X(6)  VALUE  SPACES.
    05 FILLER   PIC X(6)  VALUE "GR".
    05 FILLER   PIC X(6)  VALUE  SPACES.
    05 FILLER   PIC X(6)  VALUE "EARNED".
PROCEDURE DIVISION.
MAIN-PROGRAM.
    PERFORM HEADER.
    PERFORM FILE-IO.
    PERFORM CLOSING.
    STOP RUN.
HEADER.
    OPEN INPUT myInFile
         OUTPUT myOutFile.
    WRITE outRecord FROM NAME-HDR
           AFTER ADVANCING 1 LINE.
    WRITE outRecord FROM SCHOOLID-HDR
           AFTER ADVANCING 1 LINE.
    WRITE outRecord FROM COLUMN-HDR
           AFTER ADVANCING 2 LINES.
    MOVE SPACES TO outRecord.
    WRITE outRecord
          AFTER ADVANCING 1 LINE.
    FILE-IO.
       READ myInFile
          AT END
               MOVE "YES" TO EOF.
       PERFORM PROCESS-RECORD
       UNTIL EOF = "YES".
    PROCESS-RECORD.
         MOVE SPACES TO outRecord.
         MOVE Course to myCourse.
         MOVE Title to myTitle.
         MOVE Grade to myGrade.
         MOVE Earned to myEarned.
         WRITE outRecord
            AFTER ADVANCING 1 LINE.
         READ myInFile
            AT END
               MOVE "YES" TO EOF.
     CLOSING.
         CLOSE myInFile
               myOutFile.
4

1 回答 1

0

我的 COBOL 非常生锈,但我认为输入格式中的“Z”不起作用。前导零抑制仅适用于输出格式。在对数据执行任何操作之前,您可能只需要用零替换前导空格。尝试类似:

INSPECT Earned REPLACING ALL SPACES BY '0'.

读取后(并将输入格式更改为 99.99)。这是一个常见的问题,但我不记得我过去是如何处理它的。

于 2016-09-04T18:42:48.917 回答