MainframeMaster

COBOL Tutorial

COBOL CONSOLE - Quick Reference

Progress0 of 0 lessons

Overview

CONSOLE in COBOL refers to a terminal or display device that allows for user interaction and display output. It is used for creating interactive applications where users can see output and potentially provide input through the console interface, enabling user-friendly applications and real-time interaction.

Purpose and Usage

  • User interaction and input/output
  • Interactive applications
  • Real-time feedback
  • Debugging and monitoring
  • User-friendly interfaces

Syntax

CONSOLE operations follow specific syntax patterns in COBOL:

Basic CONSOLE Operations

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
* Basic console output DISPLAY "Hello, World!" UPON CONSOLE. DISPLAY "Welcome to the application" UPON CONSOLE. * Console input ACCEPT user-input FROM CONSOLE. ACCEPT user-choice FROM CONSOLE. * Console with formatting DISPLAY "Enter your name: " UPON CONSOLE. ACCEPT user-name FROM CONSOLE. DISPLAY "Hello, " user-name "!" UPON CONSOLE. * Console with multiple lines DISPLAY "Menu Options:" UPON CONSOLE. DISPLAY "1. Add Record" UPON CONSOLE. DISPLAY "2. View Records" UPON CONSOLE. DISPLAY "3. Exit" UPON CONSOLE. DISPLAY "Enter your choice: " UPON CONSOLE. ACCEPT menu-choice FROM CONSOLE.

CONSOLE is used with DISPLAY and ACCEPT statements for user interaction.

Console with Error Handling

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
* Console operations with error handling PROCEDURE DIVISION. CONSOLE-INTERACTION. * Display welcome message DISPLAY "Welcome to the COBOL Console Application" UPON CONSOLE. DISPLAY "=====================================" UPON CONSOLE. * Get user input with validation PERFORM get-user-input. * Process user input PERFORM process-user-choice. STOP RUN. GET-USER-INPUT. DISPLAY "Please enter your choice (1-3): " UPON CONSOLE. ACCEPT user-choice FROM CONSOLE. * Validate input IF user-choice IS NOT NUMERIC OR user-choice < 1 OR user-choice > 3 DISPLAY "Invalid choice. Please enter 1, 2, or 3." UPON CONSOLE. PERFORM get-user-input END-IF. PROCESS-USER-CHOICE. EVALUATE user-choice WHEN 1 DISPLAY "You selected: Add Record" UPON CONSOLE. PERFORM add-record-process WHEN 2 DISPLAY "You selected: View Records" UPON CONSOLE. PERFORM view-records-process WHEN 3 DISPLAY "You selected: Exit" UPON CONSOLE. PERFORM exit-process END-EVALUATE.

Practical Examples

Here are some practical uses of CONSOLE in COBOL:

Interactive Menu System

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
* Interactive menu system using console DATA DIVISION. WORKING-STORAGE SECTION. 01 menu-choice PIC 9. 01 user-name PIC X(30). 01 user-age PIC 9(3). 01 continue-flag PIC X VALUE "Y". PROCEDURE DIVISION. MAIN-MENU. * Display main menu DISPLAY "=== COBOL Console Application ===" UPON CONSOLE. DISPLAY "1. User Registration" UPON CONSOLE. DISPLAY "2. Data Processing" UPON CONSOLE. DISPLAY "3. Report Generation" UPON CONSOLE. DISPLAY "4. System Information" UPON CONSOLE. DISPLAY "5. Exit" UPON CONSOLE. DISPLAY "================================" UPON CONSOLE. * Get user choice DISPLAY "Enter your choice (1-5): " UPON CONSOLE. ACCEPT menu-choice FROM CONSOLE. * Process menu choice EVALUATE menu-choice WHEN 1 PERFORM user-registration WHEN 2 PERFORM data-processing WHEN 3 PERFORM report-generation WHEN 4 PERFORM system-information WHEN 5 PERFORM exit-application WHEN OTHER DISPLAY "Invalid choice. Please try again." UPON CONSOLE. PERFORM main-menu END-EVALUATE. * Continue or exit IF continue-flag = "Y" PERFORM main-menu END-IF. STOP RUN. USER-REGISTRATION. DISPLAY "=== User Registration ===" UPON CONSOLE. DISPLAY "Enter your name: " UPON CONSOLE. ACCEPT user-name FROM CONSOLE. DISPLAY "Enter your age: " UPON CONSOLE. ACCEPT user-age FROM CONSOLE. DISPLAY "Registration completed!" UPON CONSOLE. DISPLAY "Name: " user-name UPON CONSOLE. DISPLAY "Age: " user-age UPON CONSOLE. DISPLAY "Press Enter to continue..." UPON CONSOLE. ACCEPT continue-input FROM CONSOLE. DATA-PROCESSING. DISPLAY "=== Data Processing ===" UPON CONSOLE. DISPLAY "Processing data..." UPON CONSOLE. * Data processing logic here DISPLAY "Data processing completed!" UPON CONSOLE. DISPLAY "Press Enter to continue..." UPON CONSOLE. ACCEPT continue-input FROM CONSOLE. REPORT-GENERATION. DISPLAY "=== Report Generation ===" UPON CONSOLE. DISPLAY "Generating report..." UPON CONSOLE. * Report generation logic here DISPLAY "Report generated successfully!" UPON CONSOLE. DISPLAY "Press Enter to continue..." UPON CONSOLE. ACCEPT continue-input FROM CONSOLE. SYSTEM-INFORMATION. DISPLAY "=== System Information ===" UPON CONSOLE. DISPLAY "COBOL Console Application" UPON CONSOLE. DISPLAY "Version: 1.0" UPON CONSOLE. DISPLAY "Date: " FUNCTION CURRENT-DATE UPON CONSOLE. DISPLAY "Press Enter to continue..." UPON CONSOLE. ACCEPT continue-input FROM CONSOLE. EXIT-APPLICATION. DISPLAY "Thank you for using the application!" UPON CONSOLE. DISPLAY "Goodbye!" UPON CONSOLE. MOVE "N" TO continue-flag.

Interactive menu system using console for user interaction.

Data Entry Application

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
* Data entry application using console DATA DIVISION. WORKING-STORAGE SECTION. 01 customer-data. 05 customer-id PIC X(10). 05 customer-name PIC X(50). 05 customer-email PIC X(100). 05 customer-phone PIC X(15). 01 record-count PIC 9(4) VALUE 0. 01 continue-entry PIC X VALUE "Y". PROCEDURE DIVISION. DATA-ENTRY-APPLICATION. DISPLAY "=== Customer Data Entry System ===" UPON CONSOLE. DISPLAY "This application allows you to enter customer data." UPON CONSOLE. DISPLAY "===============================================" UPON CONSOLE. * Main data entry loop PERFORM UNTIL continue-entry = "N" PERFORM enter-customer-data PERFORM display-customer-data PERFORM ask-continue END-PERFORM. * Display summary PERFORM display-summary. STOP RUN. ENTER-CUSTOMER-DATA. ADD 1 TO record-count. DISPLAY "Entering customer #" record-count UPON CONSOLE. DISPLAY "=======================" UPON CONSOLE. * Get customer ID DISPLAY "Enter Customer ID: " UPON CONSOLE. ACCEPT customer-id FROM CONSOLE. * Get customer name DISPLAY "Enter Customer Name: " UPON CONSOLE. ACCEPT customer-name FROM CONSOLE. * Get customer email DISPLAY "Enter Customer Email: " UPON CONSOLE. ACCEPT customer-email FROM CONSOLE. * Get customer phone DISPLAY "Enter Customer Phone: " UPON CONSOLE. ACCEPT customer-phone FROM CONSOLE. DISPLAY-CUSTOMER-DATA. DISPLAY "Customer Data Entered:" UPON CONSOLE. DISPLAY "ID: " customer-id UPON CONSOLE. DISPLAY "Name: " customer-name UPON CONSOLE. DISPLAY "Email: " customer-email UPON CONSOLE. DISPLAY "Phone: " customer-phone UPON CONSOLE. DISPLAY "=======================" UPON CONSOLE. ASK-CONTINUE. DISPLAY "Do you want to enter another customer? (Y/N): " UPON CONSOLE. ACCEPT continue-entry FROM CONSOLE. * Validate input IF continue-entry NOT = "Y" AND continue-entry NOT = "N" DISPLAY "Invalid input. Please enter Y or N." UPON CONSOLE. PERFORM ask-continue END-IF. DISPLAY-SUMMARY. DISPLAY "=== Data Entry Summary ===" UPON CONSOLE. DISPLAY "Total customers entered: " record-count UPON CONSOLE. DISPLAY "Data entry completed!" UPON CONSOLE. DISPLAY "=========================" UPON CONSOLE.

Data entry application using console for user input and feedback.

Best Practices

  • Provide clear and informative messages to users.
  • Validate user input to prevent errors.
  • Use consistent formatting for console output.
  • Provide helpful error messages when input is invalid.
  • Use menus and prompts to guide user interaction.
  • Consider the user experience when designing console interfaces.

Common Pitfalls

  • Not validating user input, leading to program errors.
  • Providing unclear or confusing messages to users.
  • Not handling unexpected user input gracefully.
  • Creating overly complex console interfaces.
  • Not providing adequate feedback to users.

Test Your Knowledge

1. What is CONSOLE in COBOL?

  • A data type
  • A terminal or display device for user interaction
  • A file operation
  • A program structure

2. What is the primary purpose of CONSOLE in COBOL?

  • File processing
  • User interaction and display output
  • Data storage
  • Arithmetic calculations

3. How is CONSOLE typically used in COBOL?

  • With DISPLAY statements
  • With file operations
  • With arithmetic operations
  • With data declarations

4. What type of applications use CONSOLE?

  • Only batch applications
  • Interactive applications and user interfaces
  • Only file processing applications
  • Only database applications

5. What is a common use of CONSOLE in COBOL?

  • Data storage
  • User prompts and messages
  • File operations
  • Arithmetic calculations

Frequently Asked Questions