COBOL on Linux: Building an Employee Screen with GnuCOBOL
COBOL on Linux: Building an Employee Screen with GnuCOBOL
Introduction:
COBOL (Common Business-Oriented Language) might seem like a relic of the past to some, but it remains a powerful and relevant language, especially in industries that handle massive amounts of data. From financial systems to insurance platforms, COBOL continues to drive critical applications. While often associated with mainframes, COBOL development isn't limited to these large systems. With GnuCOBOL, an open-source compiler, you can develop and run COBOL programs on a Linux environment, opening up a world of possibilities for learning and practicing this valuable language.
In this blog post, we'll walk you through the process of building a simple employee screen using GnuCOBOL on Linux. This hands-on example will introduce you to fundamental COBOL concepts and demonstrate how to create a basic interactive program. We'll start with a simple screen and then show you how to easily add a new field, giving you a taste of how to modify and extend COBOL applications. Whether you're a seasoned programmer looking to brush up on your COBOL skills or a complete beginner eager to explore this language, this post is for you.
Before (Original Screen Output):
Employee ID: 12345
Employee Name: John Doe
Department: Sales
Salary: 60000.00
------------------------
Employee Details
------------------------
ID: 12345
Name: John Doe
Department: Sales
Salary: 60000.00
------------------------
After (Screen Output with Job Title):
Employee ID: 12345
Employee Name: John Doe
Department: Sales
Salary: 60000.00
Job Title: Sales Representative
------------------------
Employee Details
------------------------
ID: 12345
Name: John Doe
Department: Sales
Salary: 60000.00
Job Title: Sales Representative
------------------------
The COBOL Code (Employee Screen - Original):
IDENTIFICATION DIVISION.
PROGRAM-ID. EMPLOYEE-SCREEN.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 EMPLOYEE-RECORD.
05 EMP-ID PIC X(5). *> 5 alphanumeric characters
05 EMP-NAME PIC X(30). *> 30 alphanumeric characters
05 EMP-DEPT PIC X(20). *> 20 alphanumeric characters
05 EMP-SALARY PIC 9(6)V99. *> 6 digits, 2 decimal places
PROCEDURE DIVISION.
MAIN-PARAGRAPH.
DISPLAY "Employee ID: ".
ACCEPT EMP-ID.
DISPLAY "Employee Name: ".
ACCEPT EMP-NAME.
DISPLAY "Department: ".
ACCEPT EMP-DEPT.
DISPLAY "Salary: ".
ACCEPT EMP-SALARY.
DISPLAY "------------------------".
DISPLAY "Employee Details".
DISPLAY "------------------------".
DISPLAY "ID: " EMP-ID.
DISPLAY "Name: " EMP-NAME.
DISPLAY "Department: " EMP-DEPT.
DISPLAY "Salary: " EMP-SALARY.
DISPLAY "------------------------".
GOBACK.
The COBOL Code (Employee Screen - With Job Title):
IDENTIFICATION DIVISION.
PROGRAM-ID. EMPLOYEE-SCREEN.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 EMPLOYEE-RECORD.
05 EMP-ID PIC X(5). *> 5 alphanumeric characters
05 EMP-NAME PIC X(30). *> 30 alphanumeric characters
05 EMP-DEPT PIC X(20). *> 20 alphanumeric characters
05 EMP-SALARY PIC 9(6)V99. *> 6 digits, 2 decimal places
05 EMP-TITLE PIC X(25). *> 25 alphanumeric characters - New Field
PROCEDURE DIVISION.
MAIN-PARAGRAPH.
DISPLAY "Employee ID: ".
ACCEPT EMP-ID.
DISPLAY "Employee Name: ".
ACCEPT EMP-NAME.
DISPLAY "Department: ".
ACCEPT EMP-DEPT.
DISPLAY "Salary: ".
ACCEPT EMP-SALARY.
DISPLAY "Job Title: ". *> Prompt for Job Title
ACCEPT EMP-TITLE. *> Accept Job Title
DISPLAY "------------------------".
DISPLAY "Employee Details".
DISPLAY "------------------------".
DISPLAY "ID: " EMP-ID.
DISPLAY "Name: " EMP-NAME.
DISPLAY "Department: " EMP-DEPT.
DISPLAY "Salary: " EMP-SALARY.
DISPLAY "Job Title: " EMP-TITLE. *> Display Job Title
DISPLAY "------------------------".
GOBACK.
Explanation of the Code:
- IDENTIFICATION DIVISION: This division identifies the program.
PROGRAM-ID
gives the program a name. - DATA DIVISION: This section defines the data used in the program.
WORKING-STORAGE SECTION
is where we define variables.PIC
clauses define the format of the data (e.g.,X
for character,9
for numeric). For example,PIC X(5)
means a field that can hold 5 alphanumeric characters.PIC 9(6)V99
means a numeric field with 6 digits and 2 decimal places. - PROCEDURE DIVISION: This contains the program's logic.
MAIN-PARAGRAPH
is the starting point. DISPLAY
shows text on the screen.ACCEPT
gets input from the user.MOVE
(not used here, but common) assigns values to variables.
Setting up the Environment:
For this tutorial, we assume you have a working Linux environment. If you don't have GnuCOBOL installed, you can find installation instructions. For example, on Ubuntu, you can try: sudo apt-get install gnucobol
. For other distributions, please consult the GnuCOBOL documentation or your distribution's package manager.
Compiling and Running the Program:
- Save the code as
employee.cob
. - Compile:
cobc -x employee.cob
(The-x
flag creates an executable). - Run:
./employee
Conclusion:
Congratulations! You've built a basic employee screen using COBOL on Linux. This example demonstrated core COBOL concepts like data definitions, input/output operations, and program structure. Experiment with the code! Try adding more fields, implementing data validation, or exploring other COBOL features. For example, try adding a "Hire Date" field to the employee record. This is just the beginning of your COBOL journey. There are many resources available to help you learn more about this powerful language. Happy coding!
Need COBOL Expertise?
If you're looking for guidance on COBOL or any coding challenges, feel free to reach out! We'd love to help you tackle your COBOL projects. 🚀
Email us at: info@pacificw.com
Image: Gemini
Comments
Post a Comment