MainframeMaster
MainframeMaster

COBOL Tutorial

Progress0 of 0 lessons

COBOL Interactive Programming

Interactive programming in COBOL enables programs to communicate with users during execution, creating dynamic, user-driven applications. Unlike batch programs that run automatically without user interaction, interactive programs use ACCEPT and DISPLAY statements to receive input and provide output in real-time. This capability is essential for data entry systems, menu-driven applications, user interfaces, and any program that requires user decisions or input during execution.

This comprehensive guide will teach you how to create interactive COBOL programs using ACCEPT and DISPLAY, build menu-driven interfaces, validate user input, handle errors gracefully, and follow best practices for creating user-friendly interactive applications. Whether you're building data entry systems, user interfaces, or programs that require real-time user interaction, mastering interactive programming is crucial for modern COBOL development.

What is Interactive Programming?

Interactive programming refers to programs that communicate with users during execution. These programs:

  • Receive user input: Programs pause and wait for users to enter data or make choices
  • Provide immediate feedback: Programs display messages, prompts, and results in real-time
  • Respond dynamically: Program flow changes based on user input and choices
  • Enable user control: Users can navigate menus, enter data, and make decisions that affect program execution

Interactive programs differ from batch programs in several key ways:

Interactive vs Batch Programs Comparison
AspectInteractive ProgramsBatch Programs
User CommunicationReal-time communication with users via ACCEPT and DISPLAYNo user interaction during execution
Execution FlowDynamic flow based on user choices and inputPredetermined execution path
Input SourceTerminal/console input from usersFile input, predefined data
Output DestinationImmediate display to usersFiles, reports, databases
Use CasesData entry, menus, user interfaces, interactive toolsReports, data processing, automated jobs
Execution TimingRuns when user initiates, responds immediatelyScheduled runs, processes large volumes

ACCEPT Statement for Input

The ACCEPT statement reads input from external sources (typically the terminal or console) and stores it in a program variable. ACCEPT pauses program execution and waits for the user to enter data, then continues after the input is received.

Basic ACCEPT Syntax

cobol
1
ACCEPT identifier [FROM mnemonic-name]

Components:

  • identifier: The data item (variable) that will receive the input
  • FROM mnemonic-name: Optional. Specifies the input source (if omitted, uses standard input/terminal)

Simple ACCEPT Example

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
WORKING-STORAGE SECTION. 01 USER-NAME PIC X(30). 01 USER-AGE PIC 9(3). PROCEDURE DIVISION. MAIN-PARA. DISPLAY "Enter your name: " WITH NO ADVANCING ACCEPT USER-NAME DISPLAY "Enter your age: " WITH NO ADVANCING ACCEPT USER-AGE DISPLAY "Hello, " USER-NAME "! You are " USER-AGE " years old." STOP RUN.

In this example, the program prompts for the user's name and age, accepts the input, and then displays a personalized message. The WITH NO ADVANCING clause keeps the prompt and input on the same line for a cleaner interface.

ACCEPT for System Information

ACCEPT can also retrieve system information without requiring user input:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
WORKING-STORAGE SECTION. 01 CURRENT-DATE PIC 9(8). 01 CURRENT-TIME PIC 9(8). 01 USER-ID PIC X(20). PROCEDURE DIVISION. MAIN-PARA. ACCEPT CURRENT-DATE FROM DATE YYYYMMDD ACCEPT CURRENT-TIME FROM TIME ACCEPT USER-ID FROM USER NAME DISPLAY "Current Date: " CURRENT-DATE DISPLAY "Current Time: " CURRENT-TIME DISPLAY "User ID: " USER-ID STOP RUN.

These forms of ACCEPT retrieve information from the system rather than waiting for user input. This is useful for logging, timestamps, and system identification.

DISPLAY Statement for Output

The DISPLAY statement outputs data to the standard output device (typically the terminal or console). DISPLAY is used to show messages, prompts, results, and any information the user needs to see.

Basic DISPLAY Syntax

cobol
1
2
3
DISPLAY identifier-1 [identifier-2 ...] [UPON mnemonic-name] [WITH NO ADVANCING]

Components:

  • identifier-1, identifier-2, etc.: Data items or literals to display
  • UPON mnemonic-name: Optional. Specifies output device (if omitted, uses standard output)
  • WITH NO ADVANCING: Optional. Prevents cursor from advancing to next line

DISPLAY Examples

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
WORKING-STORAGE SECTION. 01 CUSTOMER-NAME PIC X(30) VALUE 'JOHN SMITH'. 01 ACCOUNT-BALANCE PIC 9(8)V99 VALUE 12345.67. PROCEDURE DIVISION. MAIN-PARA. *> Display literal text DISPLAY "Welcome to Customer Management System" *> Display variable values DISPLAY "Customer: " CUSTOMER-NAME DISPLAY "Balance: $" ACCOUNT-BALANCE *> Display with WITH NO ADVANCING for prompts DISPLAY "Enter customer ID: " WITH NO ADVANCING *> Display multiple items DISPLAY "ID: " CUSTOMER-ID " | Name: " CUSTOMER-NAME STOP RUN.

Creating Menu-Driven Interfaces

Menu-driven interfaces allow users to select from a list of options, making programs more user-friendly and easier to navigate. A menu typically displays options, accepts the user's choice, performs the selected operation, and loops back to show the menu again.

Basic Menu Structure

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
WORKING-STORAGE SECTION. 01 MENU-CHOICE PIC X(1). 01 CONTINUE-FLAG PIC X(1) VALUE 'Y'. 88 CONTINUE-PROGRAM VALUE 'Y'. 88 EXIT-PROGRAM VALUE 'N'. PROCEDURE DIVISION. MAIN-MENU. PERFORM UNTIL EXIT-PROGRAM PERFORM DISPLAY-MENU PERFORM GET-MENU-CHOICE PERFORM PROCESS-CHOICE END-PERFORM STOP RUN. DISPLAY-MENU. DISPLAY " " DISPLAY "=== CUSTOMER MANAGEMENT SYSTEM ===" DISPLAY "1. Add Customer" DISPLAY "2. View Customer" DISPLAY "3. Update Customer" DISPLAY "4. Delete Customer" DISPLAY "5. Exit" DISPLAY "Enter your choice (1-5): " WITH NO ADVANCING. GET-MENU-CHOICE. ACCEPT MENU-CHOICE. PROCESS-CHOICE. EVALUATE MENU-CHOICE WHEN '1' PERFORM ADD-CUSTOMER WHEN '2' PERFORM VIEW-CUSTOMER WHEN '3' PERFORM UPDATE-CUSTOMER WHEN '4' PERFORM DELETE-CUSTOMER WHEN '5' MOVE 'N' TO CONTINUE-FLAG DISPLAY "Thank you for using the system!" WHEN OTHER DISPLAY "Invalid choice. Please select 1-5." END-EVALUATE. ADD-CUSTOMER. DISPLAY "Adding customer..." *> Add customer logic here DISPLAY "Customer added successfully." VIEW-CUSTOMER. DISPLAY "Viewing customer..." *> View customer logic here UPDATE-CUSTOMER. DISPLAY "Updating customer..." *> Update customer logic here DELETE-CUSTOMER. DISPLAY "Deleting customer..." *> Delete customer logic here.

This menu structure provides a clear, navigable interface. The menu displays options, accepts the user's choice, processes it using EVALUATE, and loops back until the user chooses to exit. Each menu option calls a specific procedure to handle that function.

Advanced Menu with Validation

A more robust menu includes input validation to ensure users enter valid choices:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
WORKING-STORAGE SECTION. 01 MENU-CHOICE PIC X(1). 01 VALID-CHOICE PIC X(1) VALUE 'N'. 88 CHOICE-IS-VALID VALUE 'Y'. 88 CHOICE-IS-INVALID VALUE 'N'. 01 RETRY-COUNT PIC 9(2) VALUE 0. 01 MAX-RETRIES PIC 9(2) VALUE 3. PROCEDURE DIVISION. MAIN-MENU. PERFORM UNTIL EXIT-PROGRAM PERFORM DISPLAY-MENU PERFORM GET-VALID-CHOICE IF CHOICE-IS-VALID PERFORM PROCESS-CHOICE END-IF END-PERFORM STOP RUN. GET-VALID-CHOICE. MOVE 'N' TO VALID-CHOICE MOVE 0 TO RETRY-COUNT PERFORM UNTIL CHOICE-IS-VALID OR RETRY-COUNT >= MAX-RETRIES ACCEPT MENU-CHOICE IF MENU-CHOICE >= '1' AND MENU-CHOICE <= '5' MOVE 'Y' TO VALID-CHOICE ELSE ADD 1 TO RETRY-COUNT IF RETRY-COUNT < MAX-RETRIES DISPLAY "Invalid choice. Please enter 1-5: " WITH NO ADVANCING ELSE DISPLAY "Maximum retries exceeded. Exiting menu." MOVE 'N' TO CONTINUE-FLAG END-IF END-IF END-PERFORM.

This enhanced menu validates the user's choice, provides error messages for invalid input, limits retry attempts, and handles edge cases gracefully. This creates a more robust and user-friendly interface.

Input Validation

Input validation is critical in interactive programs to ensure data quality, prevent errors, and provide a good user experience. Validation checks input for format, range, type, and business rules before processing.

Types of Validation

Common validation types include:

  • Format Validation: Checks if input matches required format (e.g., numeric, alphanumeric, specific pattern)
  • Range Validation: Ensures values are within acceptable limits (e.g., age between 0-120, amount greater than zero)
  • Type Validation: Verifies data type (numeric, alphabetic, alphanumeric)
  • Length Validation: Checks if input meets length requirements
  • Business Rule Validation: Enforces business logic (e.g., account numbers must start with specific prefix)

Validation Pattern with Retry Loop

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
WORKING-STORAGE SECTION. 01 USER-INPUT PIC X(20). 01 NUMERIC-VALUE PIC 9(5). 01 VALID-INPUT PIC X(1) VALUE 'N'. 88 INPUT-IS-VALID VALUE 'Y'. 88 INPUT-IS-INVALID VALUE 'N'. 01 RETRY-COUNT PIC 9(2) VALUE 0. 01 MAX-RETRIES PIC 9(2) VALUE 3. 01 ERROR-MESSAGE PIC X(50). PROCEDURE DIVISION. MAIN-PARA. PERFORM GET-VALID-NUMBER IF INPUT-IS-VALID DISPLAY "You entered: " NUMERIC-VALUE ELSE DISPLAY "Could not get valid input after " MAX-RETRIES " attempts" END-IF STOP RUN. GET-VALID-NUMBER. MOVE 'N' TO VALID-INPUT MOVE 0 TO RETRY-COUNT PERFORM UNTIL INPUT-IS-VALID OR RETRY-COUNT >= MAX-RETRIES DISPLAY "Enter a number (1-99999): " WITH NO ADVANCING ACCEPT USER-INPUT PERFORM VALIDATE-INPUT IF INPUT-IS-INVALID ADD 1 TO RETRY-COUNT DISPLAY ERROR-MESSAGE IF RETRY-COUNT < MAX-RETRIES DISPLAY "Please try again. Attempts remaining: " (MAX-RETRIES - RETRY-COUNT) END-IF END-IF END-PERFORM. VALIDATE-INPUT. MOVE 'Y' TO VALID-INPUT MOVE SPACES TO ERROR-MESSAGE *> Check if input is numeric IF USER-INPUT IS NOT NUMERIC MOVE 'N' TO VALID-INPUT MOVE "Input must be numeric" TO ERROR-MESSAGE EXIT PARAGRAPH END-IF *> Convert to numeric and check range MOVE USER-INPUT TO NUMERIC-VALUE IF NUMERIC-VALUE < 1 OR NUMERIC-VALUE > 99999 MOVE 'N' TO VALID-INPUT MOVE "Number must be between 1 and 99999" TO ERROR-MESSAGE END-IF.

This validation pattern accepts input, validates it using multiple checks, displays helpful error messages, allows retries with a limit, and only proceeds when input is valid. The validation is comprehensive and user-friendly.

Complex Validation Example

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
WORKING-STORAGE SECTION. 01 CUSTOMER-ID-INPUT PIC X(10). 01 CUSTOMER-ID PIC X(10). 01 VALID-ID PIC X(1) VALUE 'N'. 88 ID-IS-VALID VALUE 'Y'. 01 RETRY-COUNT PIC 9(2) VALUE 0. 01 MAX-RETRIES PIC 9(2) VALUE 3. PROCEDURE DIVISION. MAIN-PARA. PERFORM GET-VALID-CUSTOMER-ID IF ID-IS-VALID DISPLAY "Valid customer ID accepted: " CUSTOMER-ID END-IF STOP RUN. GET-VALID-CUSTOMER-ID. MOVE 'N' TO VALID-ID MOVE 0 TO RETRY-COUNT PERFORM UNTIL ID-IS-VALID OR RETRY-COUNT >= MAX-RETRIES DISPLAY "Enter customer ID (format: CUST######): " WITH NO ADVANCING ACCEPT CUSTOMER-ID-INPUT PERFORM VALIDATE-CUSTOMER-ID IF ID-IS-VALID MOVE CUSTOMER-ID-INPUT TO CUSTOMER-ID ELSE ADD 1 TO RETRY-COUNT DISPLAY "Invalid format. Customer ID must start with 'CUST' " "followed by 6 digits." IF RETRY-COUNT < MAX-RETRIES DISPLAY "Attempts remaining: " (MAX-RETRIES - RETRY-COUNT) END-IF END-IF END-PERFORM. VALIDATE-CUSTOMER-ID. MOVE 'Y' TO VALID-ID *> Check length IF FUNCTION LENGTH(FUNCTION TRIM(CUSTOMER-ID-INPUT)) NOT = 10 MOVE 'N' TO VALID-ID EXIT PARAGRAPH END-IF *> Check prefix IF CUSTOMER-ID-INPUT(1:4) NOT = 'CUST' MOVE 'N' TO VALID-ID EXIT PARAGRAPH END-IF *> Check that remaining characters are numeric IF CUSTOMER-ID-INPUT(5:6) IS NOT NUMERIC MOVE 'N' TO VALID-ID END-IF.

This example demonstrates complex validation that checks multiple conditions: length, prefix format, and numeric suffix. The validation is thorough and provides specific error messages to guide users.

Error Handling

Effective error handling in interactive programs provides immediate feedback, prevents program crashes, and guides users to correct errors. Good error handling includes clear error messages, retry mechanisms, and graceful degradation.

Error Handling Patterns

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
WORKING-STORAGE SECTION. 01 USER-INPUT PIC X(50). 01 PROCESSING-FLAG PIC X(1) VALUE 'Y'. 88 CONTINUE-PROCESSING VALUE 'Y'. 88 STOP-PROCESSING VALUE 'N'. 01 ERROR-COUNT PIC 9(3) VALUE 0. 01 SUCCESS-COUNT PIC 9(3) VALUE 0. PROCEDURE DIVISION. MAIN-PARA. PERFORM PROCESS-WITH-ERROR-HANDLING DISPLAY "Processing complete. Successes: " SUCCESS-COUNT " Errors: " ERROR-COUNT STOP RUN. PROCESS-WITH-ERROR-HANDLING. PERFORM UNTIL STOP-PROCESSING DISPLAY "Enter data (or 'EXIT' to quit): " WITH NO ADVANCING ACCEPT USER-INPUT IF FUNCTION UPPER-CASE(USER-INPUT) = 'EXIT' MOVE 'N' TO PROCESSING-FLAG ELSE PERFORM PROCESS-INPUT END-IF END-PERFORM. PROCESS-INPUT. *> Attempt to process input IF USER-INPUT = SPACES ADD 1 TO ERROR-COUNT DISPLAY "ERROR: Input cannot be empty" ELSE IF USER-INPUT IS NUMERIC PERFORM VALIDATE-AND-PROCESS-NUMERIC ELSE PERFORM VALIDATE-AND-PROCESS-TEXT END-IF END-IF. VALIDATE-AND-PROCESS-NUMERIC. *> Additional validation and processing ADD 1 TO SUCCESS-COUNT DISPLAY "Numeric input processed successfully". VALIDATE-AND-PROCESS-TEXT. *> Additional validation and processing ADD 1 TO SUCCESS-COUNT DISPLAY "Text input processed successfully".

This error handling pattern tracks errors and successes, provides clear error messages, allows users to exit gracefully, and continues processing even when errors occur. This creates a robust, user-friendly program.

Complete Interactive Program Example

Here's a complete example that demonstrates interactive programming concepts:

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
IDENTIFICATION DIVISION. PROGRAM-ID. INTERACTIVE-CUSTOMER-SYSTEM. AUTHOR. MainframeMaster Tutorial. REMARKS. Complete interactive program demonstrating ACCEPT, DISPLAY, menu-driven interface, input validation, and error handling. DATA DIVISION. WORKING-STORAGE SECTION. 01 MENU-CHOICE PIC X(1). 01 CONTINUE-FLAG PIC X(1) VALUE 'Y'. 88 CONTINUE-PROGRAM VALUE 'Y'. 88 EXIT-PROGRAM VALUE 'N'. 01 CUSTOMER-DATA. 05 CUSTOMER-ID PIC X(10). 05 CUSTOMER-NAME PIC X(30). 05 CUSTOMER-BALANCE PIC 9(8)V99 VALUE 0. 01 INPUT-VALIDATION. 05 VALID-INPUT PIC X(1) VALUE 'N'. 88 INPUT-IS-VALID VALUE 'Y'. 88 INPUT-IS-INVALID VALUE 'N'. 05 RETRY-COUNT PIC 9(2) VALUE 0. 05 MAX-RETRIES PIC 9(2) VALUE 3. 05 ERROR-MESSAGE PIC X(80). PROCEDURE DIVISION. MAIN-PROGRAM. DISPLAY "========================================" DISPLAY " CUSTOMER MANAGEMENT SYSTEM" DISPLAY "========================================" DISPLAY " " PERFORM UNTIL EXIT-PROGRAM PERFORM DISPLAY-MAIN-MENU PERFORM GET-MENU-CHOICE PERFORM PROCESS-MENU-CHOICE END-PERFORM DISPLAY " " DISPLAY "Thank you for using the Customer Management System!" STOP RUN. DISPLAY-MAIN-MENU. DISPLAY " " DISPLAY "=== MAIN MENU ===" DISPLAY "1. Add New Customer" DISPLAY "2. View Customer Information" DISPLAY "3. Update Customer Balance" DISPLAY "4. Display All Customers" DISPLAY "5. Exit" DISPLAY " " DISPLAY "Enter your choice (1-5): " WITH NO ADVANCING. GET-MENU-CHOICE. ACCEPT MENU-CHOICE. PROCESS-MENU-CHOICE. EVALUATE MENU-CHOICE WHEN '1' PERFORM ADD-CUSTOMER WHEN '2' PERFORM VIEW-CUSTOMER WHEN '3' PERFORM UPDATE-BALANCE WHEN '4' PERFORM DISPLAY-ALL-CUSTOMERS WHEN '5' MOVE 'N' TO CONTINUE-FLAG WHEN OTHER DISPLAY "Invalid choice. Please select 1-5." END-EVALUATE. ADD-CUSTOMER. DISPLAY " " DISPLAY "=== ADD NEW CUSTOMER ===" PERFORM GET-VALID-CUSTOMER-ID IF INPUT-IS-VALID PERFORM GET-CUSTOMER-NAME IF INPUT-IS-VALID PERFORM GET-CUSTOMER-BALANCE IF INPUT-IS-VALID DISPLAY " " DISPLAY "Customer added successfully!" DISPLAY "ID: " CUSTOMER-ID DISPLAY "Name: " CUSTOMER-NAME DISPLAY "Balance: $" CUSTOMER-BALANCE END-IF END-IF END-IF. GET-VALID-CUSTOMER-ID. MOVE 'N' TO VALID-INPUT MOVE 0 TO RETRY-COUNT PERFORM UNTIL INPUT-IS-VALID OR RETRY-COUNT >= MAX-RETRIES DISPLAY "Enter Customer ID (10 characters): " WITH NO ADVANCING ACCEPT CUSTOMER-ID IF CUSTOMER-ID = SPACES ADD 1 TO RETRY-COUNT MOVE "Customer ID cannot be empty" TO ERROR-MESSAGE DISPLAY ERROR-MESSAGE ELSE IF FUNCTION LENGTH(FUNCTION TRIM(CUSTOMER-ID)) < 5 ADD 1 TO RETRY-COUNT MOVE "Customer ID must be at least 5 characters" TO ERROR-MESSAGE DISPLAY ERROR-MESSAGE ELSE MOVE 'Y' TO VALID-INPUT END-IF END-IF IF INPUT-IS-INVALID AND RETRY-COUNT < MAX-RETRIES DISPLAY "Please try again. Attempts remaining: " (MAX-RETRIES - RETRY-COUNT) END-IF END-PERFORM. GET-CUSTOMER-NAME. MOVE 'N' TO VALID-INPUT MOVE 0 TO RETRY-COUNT PERFORM UNTIL INPUT-IS-VALID OR RETRY-COUNT >= MAX-RETRIES DISPLAY "Enter Customer Name (max 30 characters): " WITH NO ADVANCING ACCEPT CUSTOMER-NAME IF CUSTOMER-NAME = SPACES ADD 1 TO RETRY-COUNT DISPLAY "Customer name cannot be empty" ELSE MOVE 'Y' TO VALID-INPUT END-IF END-PERFORM. GET-CUSTOMER-BALANCE. MOVE 'N' TO VALID-INPUT MOVE 0 TO RETRY-COUNT PERFORM UNTIL INPUT-IS-VALID OR RETRY-COUNT >= MAX-RETRIES DISPLAY "Enter Customer Balance: " WITH NO ADVANCING ACCEPT CUSTOMER-BALANCE IF CUSTOMER-BALANCE IS NOT NUMERIC ADD 1 TO RETRY-COUNT DISPLAY "Balance must be numeric" ELSE IF CUSTOMER-BALANCE < 0 ADD 1 TO RETRY-COUNT DISPLAY "Balance cannot be negative" ELSE MOVE 'Y' TO VALID-INPUT END-IF END-IF END-PERFORM. VIEW-CUSTOMER. DISPLAY " " DISPLAY "=== VIEW CUSTOMER ===" DISPLAY "Customer ID: " CUSTOMER-ID DISPLAY "Customer Name: " CUSTOMER-NAME DISPLAY "Balance: $" CUSTOMER-BALANCE. UPDATE-BALANCE. DISPLAY " " DISPLAY "=== UPDATE CUSTOMER BALANCE ===" DISPLAY "Current balance: $" CUSTOMER-BALANCE PERFORM GET-CUSTOMER-BALANCE IF INPUT-IS-VALID DISPLAY "Balance updated to: $" CUSTOMER-BALANCE END-IF. DISPLAY-ALL-CUSTOMERS. DISPLAY " " DISPLAY "=== ALL CUSTOMERS ===" DISPLAY "ID: " CUSTOMER-ID " | Name: " CUSTOMER-NAME " | Balance: $" CUSTOMER-BALANCE.

This complete example demonstrates all key interactive programming concepts: menu-driven interface, input validation with retry loops, error handling, user prompts, and structured program flow. It provides a solid foundation for building interactive COBOL applications.

Best Practices for Interactive Programming

Follow these best practices to create effective, user-friendly interactive programs:

  • Always validate input: Never trust user input. Validate format, range, type, and business rules before processing
  • Provide clear prompts: Use descriptive prompts that explain what input is expected and in what format
  • Use WITH NO ADVANCING for prompts: Keep prompts and input on the same line for a cleaner interface
  • Limit retry attempts: Prevent infinite loops by limiting the number of retry attempts for invalid input
  • Display helpful error messages: Error messages should explain what went wrong and how to fix it
  • Use condition names (88-level): Make code more readable with meaningful condition names like INPUT-IS-VALID
  • Structure menus clearly: Use numbered or lettered options, clear descriptions, and always provide an exit option
  • Handle all input scenarios: Consider edge cases like empty input, invalid formats, and boundary values
  • Provide immediate feedback: Confirm actions, show results, and acknowledge user input
  • Keep interactions simple: Don't overwhelm users with too many options or complex navigation
  • Test thoroughly: Test with valid input, invalid input, edge cases, and various user scenarios
  • Use meaningful variable names: Names should clearly indicate purpose (e.g., MENU-CHOICE, VALID-INPUT)

Common Interactive Programming Patterns

Pattern 1: Simple Prompt and Accept

cobol
1
2
3
DISPLAY "Enter value: " WITH NO ADVANCING ACCEPT USER-VALUE DISPLAY "You entered: " USER-VALUE

Pattern 2: Validation Loop

cobol
1
2
3
4
5
6
7
8
9
MOVE 'N' TO VALID-INPUT PERFORM UNTIL INPUT-IS-VALID DISPLAY "Enter value: " WITH NO ADVANCING ACCEPT USER-VALUE PERFORM VALIDATE-INPUT IF INPUT-IS-INVALID DISPLAY "Invalid input. Please try again." END-IF END-PERFORM

Pattern 3: Menu Loop

cobol
1
2
3
4
5
6
MOVE 'Y' TO CONTINUE-FLAG PERFORM UNTIL EXIT-PROGRAM PERFORM DISPLAY-MENU ACCEPT MENU-CHOICE PERFORM PROCESS-CHOICE END-PERFORM

Pattern 4: Confirmation Pattern

cobol
1
2
3
4
5
6
7
DISPLAY "Are you sure? (Y/N): " WITH NO ADVANCING ACCEPT CONFIRM-RESPONSE IF FUNCTION UPPER-CASE(CONFIRM-RESPONSE) = 'Y' PERFORM CONFIRMED-ACTION ELSE DISPLAY "Action cancelled." END-IF

Explain Like I'm 5: Interactive Programming

Think of interactive programming like having a conversation:

  • DISPLAY "What's your name?" is like asking a question out loud
  • ACCEPT USER-NAME is like waiting for someone to answer and remembering their answer
  • DISPLAY "Hello, " USER-NAME is like saying their name back to them
  • A menu is like a list of things you can do, and you pick one by entering its number
  • Validation is like checking if someone gave you a good answer before you use it
  • Error messages are like saying "That's not quite right, try again" when someone makes a mistake

So interactive programming is just a way for your COBOL program to have a conversation with the user - asking questions, getting answers, and responding based on what the user says!

Practice Exercises

Complete these exercises to reinforce your understanding:

Exercise 1: Simple Interactive Program

Create a program that asks for the user's name, age, and favorite color, then displays a personalized message using all three pieces of information.

Exercise 2: Number Guessing Game

Create an interactive number guessing game where the program generates a random number (or uses a fixed number), prompts the user to guess, provides feedback (too high, too low, correct), and allows multiple guesses until the user gets it right.

Exercise 3: Calculator Menu

Create a menu-driven calculator that allows users to select operations (add, subtract, multiply, divide), enter two numbers, perform the calculation, display the result, and return to the menu. Include input validation for numeric input.

Exercise 4: Data Entry System

Create a data entry system that accepts customer information (ID, name, email, phone) with validation for each field. Validate that IDs are alphanumeric, names are alphabetic, emails contain '@', and phone numbers are numeric. Allow retries for invalid input.

Exercise 5: Multi-Level Menu

Create a program with a main menu and sub-menus. The main menu has options like "Customer Management" and "Order Management". Each option leads to a sub-menu with specific functions. Include navigation back to the main menu and an exit option at each level.

Test Your Knowledge

1. What is the primary purpose of interactive programming in COBOL?

  • To process large files automatically
  • To communicate with users during program execution
  • To perform calculations faster
  • To optimize memory usage

2. Which statement is used to get input from the user in COBOL?

  • DISPLAY
  • ACCEPT
  • READ
  • WRITE

3. What does WITH NO ADVANCING do in a DISPLAY statement?

  • Prevents output from being displayed
  • Prevents the cursor from advancing to the next line
  • Displays output in uppercase
  • Displays output without formatting

4. How do you create a menu-driven interface in COBOL?

  • Use only IF statements
  • Display menu options, accept user choice, use EVALUATE or IF to process choice, loop back to menu
  • Use only PERFORM statements
  • Use only DISPLAY statements

5. What is the best way to validate user input in an interactive program?

  • Accept input and process it immediately
  • Accept input, validate it with IF statements, use a retry loop if invalid, limit retry attempts
  • Only check if input is numeric
  • Skip validation to improve performance

6. What is the difference between interactive and batch COBOL programs?

  • There is no difference
  • Interactive programs communicate with users during execution, batch programs run without user interaction
  • Interactive programs are faster
  • Batch programs use ACCEPT and DISPLAY

7. How do you create a loop that continues until valid input is received?

  • Use PERFORM VARYING
  • Use PERFORM UNTIL with a validation flag
  • Use GOTO statements
  • You cannot create such a loop

8. What should you always include when validating user input?

  • Only format checking
  • Format checking, range validation, error messages, retry limits, and clear prompts
  • Only range checking
  • No validation needed

Related Concepts

Related Pages