Bad syntax with my loop, but not throwing an error in COBOL

為{幸葍}努か 提交于 2019-12-07 16:58:26

You're suffering from "drop-through" or "fall-through".

I'll go into that shortly, but first a few other things.

Your title: Bad syntax with my loop, but not throwing an error in COBOL. My advice for questions is two write the title last, then you are more likely to avoid this problem. There is no "syntax" issue and nothing for the compiler to assist you with.

Secondly, your problem description: "Having some trouble with my read loop, it will not continue through the loop."

The meaning of that is unclear. We read one meaning into it (the loop does not get entered at all) and you meant another (one record only, the first on the file, appears in the output).

The importance of this is as follows: if the explanation of the existing code does not exactly obtain the output achieved, then that is certainly not a full explanation of the problem: don't change a line of code until you know the full explanation.

Well, OK, but often you want to make some change to aid problem determination, or just to poke about in the hope that some magic will happen and the problem will be fixed. Well, don't do it to the original program. Copy the program. We don't want to be pointing out the side-effects of the bodges and hacks you've attempted, if you want to do that.

On our reading, the Y/Y and N/N problem explained what (we thought) you were getting. However, it does not explain the actual output. How would we know that? You haven't included any output.

Sometimes the problem is an "empty" file, or a file with only one record, or indeed a "corrupt" file (tip it a fiver and it'll take a dive in the third). How, other than their being unlikely, do we exclude those if you don't include sample data?

Then we need to see the code that isn't working. However, if you knew what it was that wasn't working, most likely you'd get to the answer. So, you have to do your best to pare down the code to the smallest amount which still exhibits the problem.

So, a good title, finalised once the question is complete; accurate problem statement; sample input data, actual output, expected output; error messages (in full, with error-codes) if any; minimal sample code which produces the problem.

Often, in the good preparation of a good question you'll get to the answer yourself anyway, before you even need to ask.

A good way is to explain your problem to someone else. Reading and speaking you will read more than simply reading (to yourself) and assuming. Even if you have no colleague/friend available, just pretend you have a Cardboad-Cutout Programmer (can be an inflatable one, like the autopilot always is in comedy films), or an egg-cup, or a rubber-duck (apparently this one works as well).

Oh, and I'll update my answer on your other question.

So, the "fall-through" or "drop-through".

    MOVE X                      TO Y
    .
some-paragraph-name.
    MOVE A                      TO B
    ...

There you see a paragraph name. How does control pass to the line immediately after the paragraph name? There are three valid, legal and usable ways for ordinary code (non-Declaratives, non-SORT-procedure): after MOVE X is executed, if the code is not within a PERFORM whose range finishes at "some-paragraph-name" then the MOVE A will be next; a GO TO some-paragraph-name; a PERFORM (in some variation) of some-paragraph-name.

(Note, what applies to paragraphs above applies equally to SECTIONs in the PROCEDURE DIVISION).

This is not like many other languages where a "function" or a "sub-routine" would be "protected" from the main flow of code, they could not be "fallen into" or GO TO'd.

In COBOL, all three methods of getting there are entirely valid, and entirely valid even for the same paragraph-name in the same program (valid doesn't, of course, mean good practice: good practice would preclude such use).

Since all methods are entirely valid, the compiler has nothing to say when you use one of the methods, even when its use was unintended (how would the compiler know that?).

Here's your problem:

PERFORM 500-CLOSE-FILES.

100-OPEN-FILES.

I've highlighted the line, unfortunately since there is nothing on the line it doesn't show up.

    PERFORM 500-CLOSE-FILES
    GOBACK (or STOP RUN, or EXIT PROGRAM, but GOBACK is better)
    .
100-OPEN-FILES.

So, your program was running, processing the entire input file a record at a time, writing the data to the output file, a record at a time. All approximately OK.

Then you closed the files.

Then you dropped through to 100-OPEN-FILES, and BANG! your output is wiped. Then it does the first read. Then does 300-, just the once, so processing one record before reading the next, then drops into 310-, then 400-, then 500- which closes the files and falls off the end of the program, bringing execution to a halt.

Depending on compiler, actually, there should have been at least one termination statement in the program and a failure on falling off the program. However, the compiler you are using doesn't do that (or you've told it not to do it with options/switches).

Add the GOBACK/STOP RUN/EXIT PROGRAM and your program will work. Generally.

How could you have spotted this yourself? Counting the input and output records and DISPLAYing the totals at the end of the program often helps. If you use the FILE STATUS you get less suspicious of it being a dodgy file. Good formatting, use of 88s, as Bruce already suggested.

Two obvious problems:

  1. The read statements2. look to be in the wrong columns
  2. The no loop statement
  3. You need to set W01-DATA-REMAINS-SWITCH to Y before you start

Try

100-OPEN-FILES.
   OPEN INPUT F01-INPUT-FILE
   OUTPUT F02-PRINT-FILE
   READ F01-INPUT-FILE
      AT END MOVE "N" TO W01-DATA-REMAINS-SWITCH
   end-Read

   PERFORM UNTIL W01-DATA-REMAINS-SWITCH = "N"

       ....

       READ F01-INPUT-FILE
          AT END MOVE "N" TO W01-DATA-REMAINS-SWITCH
       end-read
  end-perform

Also

  1. Look up 88 levels and the set verb e.g.

    SET EOF            to true
    
  2. Find more out how cobol should be formatted

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!