You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在COBOL中创建正确的非数值型待办列表?

Fixing Your COBOL To-Do List with Non-Numeric Items

Let's walk through fixing your to-do list program—you're running into issues because COBOL treats string operations very differently from numeric ones, plus a few other easy-to-fix missteps. Here's how to build a working non-numeric list:

Key Issues in Your Original Code

  • You’re using COMPUTE (designed for numeric math) to concatenate/subtract strings—COBOL has dedicated string-handling commands for this task.
  • Individual ITEM1 to ITEM10 variables make the code repetitive and hard to maintain; a table (array) is the far better approach here.
  • You’re checking the ANSWER variable without first accepting user input for it.
  • DELETE is misused—COBOL’s DELETE command works for files or indexed tables, not standalone variables.

Corrected Program with Explanations

We’ll use a table to store all to-do items, then handle string concatenation and completion tracking properly:

IDENTIFICATION DIVISION.
PROGRAM-ID. TODO-LIST.
DATA DIVISION.
WORKING-STORAGE SECTION.
    * Define a table (array) for 10 to-do items
    01 TODO-LIST.
        05 TODO-ITEM OCCURS 10 TIMES PIC A(20).
        05 TODO-COUNT PIC 9(2) VALUE 10. * Track active items in the list
    * Variables for user input and display
    01 ANSWER PIC A(3) VALUE SPACES.
    01 DISPLAY-LINE PIC A(200). * For concatenated list display
    01 I PIC 9(2) VALUE 1. * Loop counter
    01 J PIC 9(2) VALUE 1. * Helper counter for shifting items

PROCEDURE DIVISION.
0100-MAIN-PROCEDURE.
    DISPLAY "Hello, what needs doing today?".
    DISPLAY "You can have up to 10 items on your list.".
    PERFORM 0200-GET-MORNING-ITEMS.
    PERFORM 0300-GET-AFTERNOON-ITEMS.
    PERFORM 0400-DISPLAY-FULL-LIST.
    PERFORM 0500-CHECK-ITEMS-OFF UNTIL TODO-COUNT = 0.
    DISPLAY "All items completed! Have a great day!".
    STOP RUN.

0200-GET-MORNING-ITEMS.
    DISPLAY "Let's start with the 1st 5 things you want to do in the morning:".
    PERFORM VARYING I FROM 1 BY 1 UNTIL I > 5
        DISPLAY "Item ", I, ": ".
        ACCEPT TODO-ITEM(I).
    END-PERFORM.

0300-GET-AFTERNOON-ITEMS.
    DISPLAY "Now for the afternoon (next 5 items):".
    PERFORM VARYING I FROM 6 BY 1 UNTIL I > 10
        DISPLAY "Item ", I, ": ".
        ACCEPT TODO-ITEM(I).
    END-PERFORM.

0400-DISPLAY-FULL-LIST.
    DISPLAY "Here's your current list:".
    * Concatenate all active items into a display line using STRING
    STRING "1. " TODO-ITEM(1) ", 2. " TODO-ITEM(2) ", 3. " TODO-ITEM(3)
           ", 4. " TODO-ITEM(4) ", 5. " TODO-ITEM(5) ", 6. " TODO-ITEM(6)
           ", 7. " TODO-ITEM(7) ", 8. " TODO-ITEM(8) ", 9. " TODO-ITEM(9)
           ", 10. " TODO-ITEM(10) INTO DISPLAY-LINE
    END-STRING.
    DISPLAY DISPLAY-LINE.

0500-CHECK-ITEMS-OFF.
    PERFORM VARYING I FROM 1 BY 1 UNTIL I > TODO-COUNT
        IF TODO-ITEM(I) NOT = SPACES
            DISPLAY "Have you completed ", TODO-ITEM(I), "? (yes/no)".
            ACCEPT ANSWER.
            IF ANSWER = "YES" OR ANSWER = "yes"
                DISPLAY "Checked-off!".
                * Shift remaining items up to "remove" the completed one
                PERFORM VARYING J FROM I BY 1 UNTIL J >= TODO-COUNT
                    MOVE TODO-ITEM(J+1) TO TODO-ITEM(J).
                END-PERFORM.
                * Clear the last active item and decrement the count
                MOVE SPACES TO TODO-ITEM(TODO-COUNT).
                SUBTRACT 1 FROM TODO-COUNT.
                * Redisplay the updated list for clarity
                PERFORM 0400-DISPLAY-FULL-LIST.
                * Reset loop index to check the now-shifted item at position I
                SUBTRACT 1 FROM I.
            ELSE
                DISPLAY "No problem, we'll check this later!".
            END-IF.
        END-IF.
    END-PERFORM.

What Changed:

  1. Table (Array) Usage: TODO-ITEM OCCURS 10 TIMES creates a single array for all items, making loops and updates far cleaner than managing 10 separate variables.
  2. String Concatenation: Used STRING instead of COMPUTE to safely combine alphanumeric items into a readable display line.
  3. Proper Input Handling: Added ACCEPT ANSWER immediately after asking the completion question, so we actually capture user input before checking it.
  4. "Deleting" Completed Items: Instead of misusing DELETE, we shift remaining items up to overwrite the completed one, then clear the last position and reduce the active item count. This simulates removing items from the list naturally.
  5. Loops: Replaced repetitive individual item paragraphs with PERFORM VARYING loops to handle all items uniformly, cutting down on redundant code.

内容的提问来源于stack exchange,提问作者futurepuzzle

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 20:27:44