Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using screens and adding two numbers - addda.cbl

Similar presentations


Presentation on theme: "Using screens and adding two numbers - addda.cbl"— Presentation transcript:

1 Using screens and adding two numbers - addda.cbl
Please use speaker notes for additional information! The program discussed in this presentation uses the basic Display and Accept statements to take in two numbers. The numbers are then added together and the answer is displayed. When the user has viewed the answer, enter can be pressed to terminate the program.

2 IDENTIFICATION DIVISION.
ADDDA.CBL IDENTIFICATION DIVISION. PROGRAM-ID. ADDPROG. AUTHOR. GROCER. *ENVIRONMENT DIVISION. DATA DIVISION. WORKING-STORAGE SECTION. 01 INPUT-AREA. 05 GET-ACCEPT-ANS PIC X VALUE SPACES. 05 FIRST-NUMBER PIC VALUE 0. 05 SECOND-NUMBER PIC VALUE 0. 05 ADD-ANS PIC VALUE 0. PROCEDURE DIVISION. MAINLINE. DISPLAY "ENTER THE FIRST NUMBER (UNDER 1000)". ACCEPT FIRST-NUMBER. DISPLAY "ENTER THE SECOND NUMBER (UNDER 1000)". ACCEPT SECOND-NUMBER. ADD FIRST-NUMBER TO SECOND-NUMBER GIVING ADD-ANS. DISPLAY "THE ANSWER IS " ADD-ANS. DISPLAY "PRESS ENTER TO END THE PROGRAM". ACCEPT GET-ACCEPT-ANS. STOP RUN. This is the code for the program that is being discussed.

3 ADDDA.CBL I want to open an existing program to compile and execute, so I will select Open for edit... If you want to key in a new program then click new and start keying in the program. If the program exists and you want to bring it up to either make changes or to compile and execute, then click Open for edit...

4 ADDDA.CBL Click on addda.cbl and then click OK or simply double click on addda.cbl. Note that when you open for edit, only source programs with a .cbl extension are shown. In this example, I am looking at the a drive and there is only one program there.

5 ADDDA.CBL This shows the program that was opened, addda.cbl.
Note that in the DATA DIVISION, there is a WORKING-STORAGE SECTION that contains an area called 01 INPUT-AREA. There is no picture associated with this area because it is used to group other entries. The picture is only given at the lowest level when you are defining the actual piece of data. In this program I have GET-ACCEPT-ANS which is a one character alphanumeric field (can accept anything) with an initial value of spaces. This means that when the program starts, this one character will contain a space. There are also two areas to accept numbers that are entered that we want to add together. These two areas are called FIRST-NUMBER and SECOND-NUMBER. Since I want numbers that are no bigger than three characters and since I want numeric data the picture I am using is Just as an X means alphanumeric data, a 9 means numeric data only. There is also an A that means alphabetic data only but this is rarely used. I have also decided to give these two fields an initial value of 0. When the user enters data, these fields will hold the user input, but when the program starts they will have an initial value of 0. Finally I have set up an area in memory called ADD-ANS that will hold the result of the calculation that I am going to do. It also has a numeric picture with an initial value of 0. However since I am adding two numbers with a maximum of 3 digits, the answer needs to be 4 digits to ensure that I can hold the entire answer. The answer will be put here when the calculation is done and then it will be displayed on the screen so the user can see the answer.

6 ADDDA.CBL This is the same image as the previous slide. Here I want to discuss the PROCEDURE DIVISION. There are four commands being used: DISPLAY, ACCEPT, ADD and STOP RUN. The commands are all located within the MAINLINE paragraph. The program will execute the command lines in order when the program is run. First it will display a request for the first number, then it will accept the number and store it in memory under the name FIRST-NUMBER (defined in WORKING-STORAGE). Then it will display a request for the second number and accept and store that in SECOND-NUMBER. Then it will add the two numbers together and store the answer in ADD-ANS. Next the answer and some messages will be displayed on the screen. The ACCEPT GET-ACCEPT-ANS will wait for the user to respond. When the user presses ENTER, the program will execute STOP RUN and the program will run. Remember, before any of this happens the source program needs to be compiled and an object program produced. It is when the object program is executed (run) that we will see the program in action.

7 ADDDA.CBL To compile the program, go to Compile/Run and select Compile Program. Remember you may see slightly different menu options in the lab.

8 ADDDA.CBL The program has been successfully compiled and the line numbers have been assigned.

9 ADDDA.CBL After closing the source program (.cbl), I use Open for execution to open the object program (.int). I have now closed the file - to do this I did File/Close. I now want to open the object program. To do this I do File/Open for execution...

10 ADDDA.INT I am now going to select addda.int and open the file for execution.

11 ADDDA.INT This shows addda.int with the first line highlighted. It is ready to be run.

12 ADDDA.INT The second number has been entered, but ENTER has not been pressed so the cursor is waiting. When Enter has been pressed the ACCEPT of the second number will be complete. Note that the user enters the number and then presses enter. In this case, we are entering a two digit number, we might be entering a one digit number. Pressing enter says we are done.

13 ADDDA.INT PROCEDURE DIVISION. MAINLINE.
DISPLAY "ENTER THE FIRST NUMBER (UNDER 1000)". ACCEPT FIRST-NUMBER. DISPLAY "ENTER THE SECOND NUMBER (UNDER 1000)". ACCEPT SECOND-NUMBER. ADD FIRST-NUMBER TO SECOND-NUMBER GIVING ADD-ANS. DISPLAY "THE ANSWER IS " ADD-ANS. DISPLAY "PRESS ENTER TO END THE PROGRAM". ACCEPT GET-ACCEPT-ANS. STOP RUN. The two numbers have been calculated and the answer has been displayed. Note that 12 plus 24 is 36. Because we specified the answer as 4 digits the two leading 0s are shown. Later we will learn ways to avoid that. We are now waiting for the user to press ENTER to end the program.

14 ADDDA.INT STOP RUN has been encountered.

15 We are ready to execute the first command (it is highlighted).
Step through I want to see data so I will use the icon with the foot step and the magnifying glass. We are ready to execute the first command (it is highlighted). We are now going to step through the program watching the instructions as they execute. This can be valuable when we are trying to figure out a logic problem. Stepping through the program can be done by pressing the icon that looks like a foot step. If we want to step through and see the data as well we use the foot step and the magnifying glass icon.

16 Step through The first message has been displayed and the second command is highlighted. The user will enter a number and it will be stored in FIRST-NUMBER.

17 As a result of the ACCEPT, 12 is now stored in FIRST-NUMBER.
Step through As a result of the ACCEPT, 12 is now stored in FIRST-NUMBER. We have now moved to the third command - when I click the icon, it will be executed and we will move to the fourth command.

18 Step through We have now moved to the forth command and the message from the third command is visible on the screen.

19 Step through The second number has now been entered and is stored in the data name I made up which is SECOND-NUMBER. Note that in this example, I entered 12 as both the first number and the second number. In the last example, I entered different numbers.

20 Step through We have now moved past the ADD so the answer is stored in ADD-ANS. Notice that you can see the content of FIRST-NUMBER, SECOND-NUMBER and ADD-ANS. However the display has not been executed (I need to click on the icon) so the answer has not yet been displayed on the screen.

21 Step through We have now moved to the line after the answer is displayed (we have not executed it yet), so the answer appears on the screen. Notice again that this DISPLAY puts out both a message and the contents of a field that was set up in WORKING-STORAGE and is stored in memory.

22 Step through Control has now moved to the ACCEPT and the last display is on the screen.

23 Step through The user has responded and control has moved to past the ACCEPT.

24 User clicks OK to end the program.
Step through User clicks OK to end the program. STOP RUN has been executed and the program ended normally. When the user click OK, the program will be completely done.


Download ppt "Using screens and adding two numbers - addda.cbl"

Similar presentations


Ads by Google