如何在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
ITEM1toITEM10variables make the code repetitive and hard to maintain; a table (array) is the far better approach here. - You’re checking the
ANSWERvariable without first accepting user input for it. DELETEis misused—COBOL’sDELETEcommand 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:
- Table (Array) Usage:
TODO-ITEM OCCURS 10 TIMEScreates a single array for all items, making loops and updates far cleaner than managing 10 separate variables. - String Concatenation: Used
STRINGinstead ofCOMPUTEto safely combine alphanumeric items into a readable display line. - Proper Input Handling: Added
ACCEPT ANSWERimmediately after asking the completion question, so we actually capture user input before checking it. - "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. - Loops: Replaced repetitive individual item paragraphs with
PERFORM VARYINGloops to handle all items uniformly, cutting down on redundant code.
内容的提问来源于stack exchange,提问作者futurepuzzle
相关产品推荐
相关产品推荐

