I'm having trouble with tables using Cobol [closed] - cobol

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
IDENTIFICATION DIVISION.
PROGRAM-ID. MP4-5.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 SEAT-DESCRIPTION.
03 FILLER PIC X(50) VALUE "01 DRIVER".
03 FILLER PIC X(50) VALUE "02 DRIVER'S ASST".
03 FILLER PIC X(50) VALUE "03".
03 FILLER PIC X(50) VALUE "04".
03 FILLER PIC X(50) VALUE "05".
03 FILLER PIC X(50) VALUE "06".
03 FILLER PIC X(50) VALUE "07".
03 FILLER PIC X(50) VALUE "08".
03 FILLER PIC X(50) VALUE "09".
03 FILLER PIC X(50) VALUE "10".
03 FILLER PIC X(50) VALUE "11".
03 FILLER PIC X(50) VALUE "12".
03 FILLER PIC X(50) VALUE "13".
03 FILLER PIC X(50) VALUE "14".
03 FILLER PIC X(50) VALUE "15".
01 MAIN-MENU PIC X.
88 RESERVATION VALUE "R".
88 VIEW-RESERVATION VALUE "V".
88 CANCEL-RESERVATION VALUE "D".
88 EXIT-PROC VALUE "E".
01 DECISION PIC X.
88 YES VALUE "Y".
88 NAY VALUE "N".
01 SEAT-NUM PIC 9(2).
01 CUST-NAME PIC X(18).
01 CUST-CNTCT PIC 9(11).
01 CHOICE PIC X.
PROCEDURE DIVISION.
MAIN-ROUTINE.
DISPLAY " ABC TRANSPORT COMPANY".
DISPLAY "__________________________________________".
DISPLAY " [R] ADD RESERVATION/S ".
DISPLAY " [V] VIEW RESERVATION/S ".
DISPLAY " [D] CANCEL RESERVATION/S ".
DISPLAY " [E] EXIT".
DISPLAY " ENTER CHOICE: " WITH NO ADVANCING.
ACCEPT MAIN-MENU.
PERFORM I-CHAIN.
I-CHAIN.
IF RESERVATION
GO TO RESERVE-PROC
ELSE IF VIEW-RESERVATION
GO TO VRESERVE-PROC
ELSE IF CANCEL-RESERVATION
GO TO CRESERVE-PROC
ELSE IF EXIT-PROC
GO TO END-PGM
ELSE
DISPLAY "INVALID OPTION"
GO TO MAIN-ROUTINE.
RESERVE-PROC.
DISPLAY "RESERVE SEAT #: "WITH NO ADVANCING.
ACCEPT SEAT-NUM.
IF SEAT-NUM IS NUMERIC
IF SEAT-NUM >2 AND SEAT-NUM <16
GO TO CONT-RES
ELSE
DISPLAY "INVALID OPTION"
PERFORM RESERVE-PROC
ELSE
DISPLAY "ENTER A NUMERIC VALUE"
GO TO RESERVE-PROC.
CONT-RES.
DISPLAY "CUSTOMER NAME : "WITH NO ADVANCING.
ACCEPT CUST-NAME.
MOVE CUST-NAME TO SEAT-NUM.
DISPLAY "CUSTOMER CONTACT #: "WITH NO ADVANCING.
ACCEPT CUST-CNTCT.
MOVE CUST-CNTCT TO SEAT-NUM.
GO TO RESER-CONT.
RESER-CONT.
DISPLAY "RESERVE MORE? [Y/N]? "WITH NO ADVANCING.
ACCEPT DECISION.
IF YES
PERFORM RESERVE-PROC
ELSE IF NAY
PERFORM MAIN-ROUTINE
ELSE
DISPLAY "INVALID OPTION"
PERFORM RESER-CONT.
VRESERVE-PROC.
DISPLAY " ABC TRANSPORT COMPANY".
DISPLAY " RESERVATION LIST".
DISPLAY "__________________________________________".
DISPLAY " "SEAT-DESCRIPTION.
CRESERVE-PROC.
DISPLAY " ABC TRANSPORT COMPANY".
DISPLAY " CANCEL RESERVATION".
DISPLAY "__________________________________________".
END-PGM.
STOP RUN.
I know the code is not complete yet but may I have some help on how to use tables because
I'm having a hard time trying to display what I want to add to my variable SEAT-NUM and any tips for deleting a string 1 by 1 would be helpful

To start you'd need something like
01 SEAT-DESCRIPTION-REDEF REDEFINES SEAT-DESCRIPTION.
03 SEAT-NUMBER-DATA OCCURS 15.
05 SEAT-NUMBER-TEXT PIC X(02).
05 FILLER PIC X(04).
05 SEAT-FILLED-BY PIC X(44).
You can now assign a value to SEAT-FILLED-BY(SEAT-NUM) to assign a name to a seat.
It's not clear what you are doing with CUST-CNTCT but a small adjustment:
01 SEAT-DESCRIPTION-REDEF REDEFINES SEAT-DESCRIPTION.
03 SEAT-NUMBER-DATA OCCURS 15.
05 SEAT-NUMBER-TEXT PIC X(02).
05 FILLER PIC X(04).
05 SEAT-FILLED-BY PIC X(32).
05 FILLER PIC X(01).
05 SEAT-CONTACT PIC X(11).
and now you can MOVE CUST-CNTCT TO SEAT-CONTACT(SEAT-NUM). which is probably close to what you want to do.
And now a little advertisement about structure.
ESCHEW GO TO.
It is NEVER necessary to use a GO TO. Always
PERFORM paragraphname [UNTIL condition].
It's not an easy discipline until you get used to it - and you'll get used to it when you have nightmares with spaghetti-code; GO TO s all over the place and PERFORM me invoking recursive code.
And if anyone ever mentions PERFORM THROUGH simply smile and nod and ignore everything they have to say. They are the epitome of evil for they are advocating layout-dependant code.

We use THROUGH in our shop, but for one purpose. We label each paragrah with a name and close that paragraph with that name with"-"EXIT" on the end.
Then when we call that paragraph we call it using the syntax
"PERFORM L2-SUB-PARAGRAPH
THROUGH L2-SUB-PARAPGRAPH-EXIT.
That gives the code a more object oriented look and makes paragraphs more modular.
Or if want to loop through a paragraph you then can code it like this:
`PERFORM -L2-LOOPING-PARA'
'THROUGH L2-LOOPING-PARA-EXIT'
'VARYING INDEX1 FROM 1 BY 1.... `

Related

Best way to create a key-value "dict" in COBOL

I'm pretty new to Cobol, and got stuck trying to create something like a python dictionary, where we pass a key and the dictionary returns its value.
Python example:
>>> dict
{'AC': 'Acre', 'AL': 'Alagoas', 'AP': 'Amapa'}
>>> dict['AC']
'Acre'
I'm trying to do this in cobol, using redefines to create two arrays (one for the keys, other for the values).
I already created the arrays, but got stucked to associate these two arrays in a key-value function, once I can only access an array with integer values.
Here goes my data division, if someone can help with code samples.
DATA DIVISION.
FILE SECTION.
WORKING-STORAGE SECTION.
01 WRK-KEYS.
02 FILLER PIC X(2) VALUE "AC".
02 FILLER PIC X(2) VALUE "AL".
02 FILLER PIC X(2) VALUE "AP".
01 WRK-TABLE-KEYS REDEFINES WRK-KEYS.
02 WRK-KEY PIC X(2) OCCURS 3 TIMES.
01 WRK-VALUES.
02 FILLER PIC X(19) VALUE "Acre".
02 FILLER PIC X(19) VALUE "Alagoas".
02 FILLER PIC X(19) VALUE "Amapa".
01 WRK-TABLE-VALUES REDEFINES WRK-VALUES.
02 WRK-VALUE PIC X(10) OCCURS 3 TIMES.
You can use a table, as shown in the example below:
IDENTIFICATION DIVISION.
PROGRAM-ID. STATES.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 STATES-TABLE.
05 FILLER PIC X(2) VALUE "AC".
05 FILLER PIC X(7) VALUE "Acre ".
05 FILLER PIC X(2) VALUE "AL".
05 FILLER PIC X(7) VALUE "Alagoas".
05 FILLER PIC X(2) VALUE "AP".
05 FILLER PIC X(7) VALUE "Amapá ".
01 RDF-STATES-TABLE REDEFINES STATES-TABLE.
05 STATE-GROUP OCCURS 3 TIMES.
10 STATE-CODE PIC X(2).
10 STATE-NAME PIC X(7).
PROCEDURE DIVISION.
DISPLAY "STATES : "STATES-TABLE.
DISPLAY 'STATE-CODE(1) : ' STATE-CODE(1).
DISPLAY 'STATE-NAME(1) : ' STATE-NAME(1).
DISPLAY 'STATE-CODE(2) : ' STATE-CODE(2).
DISPLAY 'STATE-NAME(2) : ' STATE-NAME(2).
DISPLAY 'STATE-CODE(3) : ' STATE-CODE(3).
DISPLAY 'STATE-NAME(3) : ' STATE-NAME(3).
STOP RUN.
Resulting in:
$ ./states
STATES : ACAcre ALAlagoasAPAmapá
STATE-CODE(1) : AC
STATE-NAME(1) : Acre
STATE-CODE(2) : AL
STATE-NAME(2) : Alagoas
STATE-CODE(3) : AP
STATE-NAME(3) : Amapá
Remember that á uses two bytes in UTF-8.

How does COBOL actually accept numeric values?

I have a very simple COBOL code here that has a given input data and output data. The problem is that, it shows an error on line 60 which is the MOVE STUD-AGE TO AGE-OUT. and everytime I run OpenCOBOLIDE, I always get and error which is:
libcob: test.cob: 60: 'STUD-AGE' not numeric: ' '
WARNING - Implicit CLOSE of STUDENT-OUT ('C:\STUD-OUT.DAT')
WARNING - Implicit CLOSE of STUDENT-IN ('C:\STUD-IN.DAT')
And I don't know exactly what's wrong with it. Here is supposedly the input file I created:
----5---10---15---20---25---30---35---40--
00-123345 ALISON MARTIN WOLF 1912056
00-789012 KEN DENNIOS ROME 1914156
00-345678 JACK ADRIAN TOCKSIN 1622234
00-901234 EJHAYZ ALONEY 2045645
00-567890 CHARLES JOHN GUINNIVER 1813243
00-123457 JEAN MICHAEL YARTER 2034253
Here's the code to it:
IDENTIFICATION DIVISION.
PROGRAM-ID. SAMPLE.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT STUDENT-IN ASSIGN TO "C:\STUD-IN.DAT".
SELECT STUDENT-OUT ASSIGN TO "C:\STUD-OUT.DAT".
DATA DIVISION.
FILE SECTION.
FD STUDENT-IN.
01 STUD-REC.
02 STUD-NO PIC X(10).
02 STUD-NAME PIC X(25).
02 STUD-AGE PIC 99.
02 STUD-ALLOWANCE PIC 999V99.
FD STUDENT-OUT.
01 PRINT-REC PIC X(80).
WORKING-STORAGE SECTION.
01 HDG-1.
02 FILLER PIC X(20) VALUE SPACES.
02 FILLER PIC X(22) VALUE "WILLOW PARK UNIVERSITY".
02 FILLER PIC X(14) VALUE " OF MADAGASCAR".
01 HDG-2.
02 FILLER PIC X(9) VALUE SPACES.
02 FILLER PIC X(14) VALUE "STUDENT NUMBER".
02 FILLER PIC X(8) VALUE SPACES.
02 FILLER PIC X(12) VALUE "STUDENT NAME".
02 FILLER PIC X(15) VALUE SPACES.
02 FILLER PIC X(3) VALUE "AGE".
02 FILLER PIC X(8) VALUE SPACES.
02 FILLER PIC X(9) VALUE "ALLOWANCE".
01 PRINT-LINE.
02 FILLER PIC X(9) VALUE SPACES.
02 SNO-OUT PIC X(10).
02 FILLER PIC X(12) VALUE SPACES.
02 SNAME-OUT PIC X(25).
02 FILLER PIC X(2) VALUE SPACE.
02 AGE-OUT PIC Z9.
02 FILLER PIC X(9) VALUE SPACES.
02 ALL-OUT PIC ZZZ.99.
01 E-O-F PIC XXX VALUE "NO".
PROCEDURE DIVISION.
OPEN INPUT STUDENT-IN
OUTPUT STUDENT-OUT.
WRITE PRINT-REC FROM HDG-1 BEFORE 1 LINE.
WRITE PRINT-REC FROM HDG-2 AFTER 2 LINES.
MOVE SPACES TO PRINT-REC.
WRITE PRINT-REC AFTER 1 LINE.
PERFORM READ-RTN UNTIL E-O-F = "YES".
PERFORM CLOSE-RTN.
READ-RTN.
READ STUDENT-IN AT END MOVE "YES" TO E-O-F.
MOVE STUD-NO TO SNO-OUT.
MOVE STUD-NAME TO SNAME-OUT.
MOVE STUD-AGE TO AGE-OUT.
MOVE STUD-ALLOWANCE TO ALL-OUT.
WRITE PRINT-REC FROM PRINT-LINE AFTER 1 LINE.
CLOSE-RTN.
CLOSE STUDENT-IN, STUDENT-OUT.
STOP RUN.
What I want to achieve is just to output the file correctly but the error only inputs the HDG-1 and then the rest blank.
To answer your question: COBOL accept numeric data however you define it.
So for "text data" (as long as it isn't UTF-16 or another multibyte encoded file) PIC 99 (which says "two digits in the default USAGE DISPLAY - so one byte per digit) is perfectly fine.
As with every other language: "never trust input data" is something I can recommend. For example: someone could run this program with a file that was saved with an UTF-8 encoded character in the name and then it "looks" right but the code has an unexpected shift in its data. For COBOL things like FUNCTION TEST-NUMVAL(inp) [ignores spaces and allows decimal-point] or IS NUMERIC (strict class test) can be useful.
Using data-check you could for example also skip empty lines or leading/trailing extra data (temporary rulers, headline, summary, ...).
For the actual problem:
It looks like you feed the program with a "common" text file, but you actually did not specify this so your COBOL implementation uses the default SEQUENTIAL. Because of the missing check of the input data you did not spot this directly.
To align expectations and code:
SELECT STUDENT-IN ASSIGN TO "C:\STUD-IN.DAT"
ORGANIZATION IS LINE SEQUENTIAL.
SELECT STUDENT-OUT ASSIGN TO "C:\STUD-OUT.DAT"
ORGANIZATION IS LINE SEQUENTIAL.

I can't figure out why my filler spaces aren't being displayed in my COBOL application

Ok so I'm making this application for school, which requires a certain format of spacing between the entries read in from .txt file. I've created the header using the filler term and the spacing works just fine, however when I apply the same method to the formatting of the records imported from the .txt it doesn't seem to work. I've tried everything under the sun and I can't make it work for the life of me!
This is what the output looks like now:
PARTNUMBER PARTNAME QUANTITY VALUE
1111111screws robertson 10 43210200Ajax
2222222screws robertson 08 41000100Ajax
2222233screws robertson 06 43210200Ajax
3333333screws robertson 04 41000100Ajax
4444444bolts dead 10 43210200Robo
5555555bolts dead 80 01000100Robo
But, it should be something like:
PARTNUMBER PARTNAME QUANTITY VALUE
1111111 screws robertson 10 43210200 Ajax
2222222 screws robertson 08 41000100 Ajax
2222233 screws robertson 06 43210200 Ajax
3333333 screws robertson 04 41000100 Ajax
4444444 bolts dead 10 43210200 Robo
5555555 bolts dead. 80 01000100 Robo
Below is the code that I think I need to make this happen, but again I'm just not sure why it isn't working
FILE SECTION.
FD INVENT-FILE-IN.
01 INVENT-RECORD-IN PIC X(49).
WORKING-STORAGE SECTION.
01 DISPLAY-HEADERS.
05 DISPLAY-PART-NUMBER PIC A(11)
VALUE "PARTNUMBER".
05 FILLER PIC X(1).
05 DISPLAY-PART-NAME PIC A(9)
VALUE "PARTNAME".
05 FILLER PIC X(4).
05 DISPLAY-QUANTITY PIC A(8)
VALUE "QUANTITY".
05 FILLER PIC X(2).
05 DISPLAY-VALUE PIC A(5)
VALUE "VALUE".
01 DISPLAY-RECORDS.
05 WS-INVENTORY-PART-NUMBER PIC 9(7).
05 FILLER PIC X(4) VALUE SPACES.
05 WS-INVENTORY-PART-NAME PIC X(20).
05 FILLER PIC X(4) VALUE SPACES.
05 WS-INVENTORY-QUANTITY PIC 9(4).
05 FILLER PIC X(2) VALUE SPACES.
05 WS-INVENTORY-VALUE PIC 9(8).
05 FILLER PIC X(1) VALUE SPACES.
05 WS-INVENTORY-SUPPLIER-CODE PIC X(5).
PROCEDURE DIVISION.
100-PROCESS-INVENTORY-FILE.
PERFORM 201-OPEN-INVENT-FILE.
PERFORM 202-DISPLAY-HEADER.
PERFORM 204-INPUT-INVENT-FILE
PERFORM 206-DISPLAY-RECORDS
UNTIL EOF-SWITCH = "Y".
PERFORM 205-TERMINATE-INVENTORY-FILE.
STOP RUN.
201-OPEN-INVENT-FILE.
OPEN INPUT INVENT-FILE-IN.
202-DISPLAY-HEADER.
DISPLAY DISPLAY-HEADERS.
206-DISPLAY-RECORDS.
MOVE INVENT-RECORD-IN TO DISPLAY-RECORDS.
DISPLAY DISPLAY-RECORDS.
READ INVENT-FILE-IN
AT END
MOVE "Y" TO EOF-SWITCH
NOT AT END
COMPUTE READ-COUNTER = READ-COUNTER + 1
END-READ.
204-INPUT-INVENT-FILE.
READ INVENT-FILE-IN
AT END
MOVE "Y" TO EOF-SWITCH
NOT AT END
COMPUTE READ-COUNTER = READ-COUNTER + 1
END-READ.
205-TERMINATE-INVENTORY-FILE.
CLOSE INVENT-FILE-IN.
As previously stated in the comments, in paragraph 206-DISPLAY-RECORDS, you are moving the entire input record to DISPLAY-RECORDS.
The problem here is that your input record is not formatted the same as your output record. This just means that you have to format it yourself. The easiest way to do this is to define your input input record differently. Something like this should do the trick:
FILE SECTION.
FD INVENT-FILE-IN.
01 INVENT-RECORD-IN.
05 INVENT-PART-NUMBER PIC 9(7).
05 INVENT-PART-NAME PIC X(20).
05 INVENT-QUANTITY PIC 9(4).
05 INVENT-VALUE PIC 9(8).
05 INVENT-SUPPLIER-CODE PIC X(5).
From here, its as easy as moving this fields to their equivalent spot in you DISPLAY-RECORDS:
206-DISPLAY-RECORDS.
MOVE INVENT-PART-NUMBER TO WS-INVENTORY-PART-NUMBER
MOVE INVENT-PART-NAME TO WS-INVENTORY-PART-NAME
MOVE INVENT-QUANTITY TO WS-INVENTORY-QUANTITY
MOVE INVENT-VALUE TO WS-INVENTORY-VALUE
MOVE INVENT-SUPPLIER-CODE TO WS-INVENTORY-SUPPLIER-CODE
READ INVENT-FILE-IN
AT END
MOVE "Y" TO EOF-SWITCH
NOT AT END
COMPUTE READ-COUNTER = READ-COUNTER + 1
END-READ.

COBOL COMPUTE decimal values from a file

Trying to understand compute. Would it be correct to calculate the sum of the earned credits using FSemesterTotal which is a PIC 99V99 like this? COMPUTE FSemesterTotal = Earned + Earned. I think there is supposed to be a counter in my loop to check if i read in the first earned value so i can add it to the second value coming in not sure how to accomplish this in COBOL.
Currently my input is like this,
CMPS161 ALGORITHM DSGN/IMPLMNT I A 3.00
ENGL322 INTRO TO PROF/TECH WRITING A 3.00
MATH241 ELEM STATISTICS B 3.00
ART 106 SURV WORLD ART HIST II A 3.00
BIOL152 GENERAL BIOL LAB I B 1.00
CMPS257 DISCRETE STRUCTURE A 3.00
CMPS28O ALGORITHM DSGN/IMPLEM II B 3.00
CMPS290 COMPUTER ORGANIZATION A 3.00
CMPS390 DATA STRUCTURES B 3.00
GBIO153 GENERAL BIOL II B 3.00
CMPS294 INTERNET PROGRAMMING B 3.00
CMPS315 SYSTEM ADMINISTRATION A 3.00
CMPS329 COMPUTER NETWORKING SECURITY A 3.00
CMPS383 INFORMATION SYSTEMS A 3.00
CMPS415 INTERGRATED TECH SYSTEMS B 3.00
COBOL CODE
IDENTIFICATION DIVISION.
PROGRAM-ID. P2.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT myInFile ASSIGN TO "P2In.dat".
SELECT myOutFile ASSIGN TO "P2Out.dat".
DATA DIVISION.
FILE SECTION.
FD myInFile.
01 inRecord.
02 Course PIC X(13).
02 Title PIC X(32).
02 Grade PIC X(4).
02 Earned PIC 9V99.
02 FILLER PIC X(3).
FD myOutFile.
01 outRecord.
02 myCourse PIC X(13).
02 myTitle PIC X(32).
02 myGrade PIC X(4).
02 myEarned PIC 9.99.
02 FILLER PIC X(3).
WORKING-STORAGE SECTION.
01 E0F PIC X(3) VALUE "NO ".
01 NAME-HDR.
05 FILLER PIC X(10) VALUE "NAME HERE ".
01 SCHOOLID-HDR.
05 FILLER PIC X(8) VALUE "SCHOOLID"
01 COLUMN-HDR.
05 CCourse PIC X(6) VALUE "COURSE".
05 CSpace PIC X(7) VALUE SPACES.
05 HTitle PIC X(5) VALUE "TITLE".
05 HSpace PIC X(27) VALUE SPACES.
05 CGrade PIC XX VALUE "GR".
05 CSpace PIC XXX VALUE SPACES.
05 CEarned PIC X(6) VALUE "EARNED".
05 QSpace PIC X(4) VALUE SPACES.
05 Qpts PIC X(4) VALUE "Qpts".
01 FOOTER-SMS.
05 FSemester PIC X(28) VALUE " SEMESTER".
05 FSpaces PIC x(21) VALUE SPACES.
05 FSemesterTotal PIC 99V99.
01 FOOTER-CUMUL.
05 FCumulative PIC X(30) VALUE" CUMULATIVE".
05 FSpaces PIC X(19) VALUE SPACES.
05 FCumulTotal PIC 99V99.
01 QPTS-VAL.
05 QSpace PIC X(5) VALUE SPACES.
05 QPtsValue PIC 99V99.
01 GPA.
05 GSpace PIC XX VALUE SPACES.
05 GpaScore PIC 9.99.
PROCEDURE DIVISION.
MAIN-PROGRAM.
PERFORM HEADER.
PERFORM FILE-IO.
PERFORM CLOSING.
STOP RUN.
HEADER.
OPEN INPUT myInFile
OUTPUT myOutFile.
WRITE outRecord FROM NAME-HDR.
WRITE outRecord FROM SCHOOLID-HDR
AFTER ADVANCING 1 LINE.
WRITE outRecord FROM COLUMN-HDR
AFTER ADVANCING 2 LINES.
MOVE SPACES TO outRecord.
WRITE outRecord
AFTER ADVANCING 1 LINE.
FILE-IO.
READ myInFile
AT END
MOVE "YES" TO EOF.
DISPLAY NAME-HDR.
DISPLAY SCHOOLID-HDR.
DISPLAY SPACES.
DISPLAY SPACES.
DISPLAY "FALL 2014"
DISPLAY COLUMN-HDR.
PERFORM PROCESS-RECORD
UNTIL EOF = "YES".
PROCESS-RECORD.
MOVE Course to myCourse.
MOVE Title to myTitle.
MOVE Grade to myGrade.
MOVE Earned to myEarned.
WRITE outRecord
AFTER ADVANCING 1 LINE.
READ myInFile
AT END
MOVE "YES" TO EOF.
NOT AT END
IF myCourse = "ART 106 " THEN
DISPLAY FOOTER-SMS, QPTS-VAL, GPA
DISPLAY FOOTER-CUMUL, QPTS-VAL, GPA
DISPLAY SPACES.
DISPLAY "SPRING 2015"
END-IF.
IF myCourse = "CMPS285 " THEN
DISPLAY FOOTER-SMS, QPTS-VAL, GPA
DISPLAY FOOTER-CUMUL, QPTS-VAL, GPA
DISPLAY SPACES.
DISPLAY "FALL 2015"
END-IF.
IF myCourse = "CMPS294 " THEN
DISPLAY FOOTER-SMS, QPTS-VAL, GPA
DISPLAY FOOTER-CUMUL, QPTS-VAL, GPA
DISPLAY SPACES.
DISPLAY "SPRING 2016"
END-IF.
CLOSING.
DISPLAY FOOTER-SMS, QPTS-VAL, GPA.
DISPLAY FOOTER-CUMUL, QPTS-VAL, GPA.
CLOSE myInFile
myOutFile.
The question was: "Can I use COMPUTE this way?"
The answer is:
Yes, but you likely want to add a ON SIZE ERROR to cater for a possible size overflow, just in case your input data has too many entries.
If the question behind the question is: "Will the program work?"
The answer is no:
Despite the issues Brian already pointed out: you'll need a de-editing to change the data from 9.99 (4 bytes, not usable for arithmetic) to 9v99 (3 bytes, usable for arithmetic.
And if you don't use an ISAM file which is validated by the runtime: Always validate file input (the file may be broken and you likely don't want to abend or produce wrong results).

When I open my output file in cobol, random characters appear

here is the code i am running (it is not mine, it is from my professor but I can't seem to make it work. Help please.
IDENTIFICATION DIVISION.
PROGRAM-ID. ACPTDSP1.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT GRDFILE ASSIGN TO DISK.
DATA DIVISION.
FILE SECTION.
FD GRDFILE
DATA RECORD IS GRDREC
LABEL RECORDS ARE STANDARD
VALUE OF FILE-ID IS "C:\COBOL\GRDFILE.TXT".
01 GRDREC.
05 FILLER PIC X(80).
WORKING-STORAGE SECTION.
* INPUT DATA USING *
* ACCEPT & DISPLAY *
01 NAME PIC X(25).
01 MIDGRD PIC 9V99.
01 FINGRD PIC 9V99.
01 AVE PIC 9V99.
01 ANS PIC X.
* OUTPUT FILE *
01 HDG.
05 FILLER PIC X(32) VALUE SPACES.
05 FILLER PIC X(25) VALUE "STUDENT'S GRADE".
05 FILLER PIC X(33) VALUE SPACES.
01 COLHDG.
05 FILLER PIC X(14) VALUE SPACES.
05 FILLER PIC X(12) VALUE "STUDENT NAME".
05 FILLER PIC X(28) VALUE SPACES.
05 FILLER PIC X(12) VALUE "FINAL GRADE ".
01 GRDDATA.
05 FILLER PIC X(14) VALUE SPACES.
05 NAME-OUT PIC X(25).
05 FILLER PIC X(20) VALUE SPACES.
05 AVE-OUT PIC 9.99.
SCREEN SECTION.
01 CLRSCR.
05 BLANK SCREEN.
PROCEDURE DIVISION.
MAIN-RTN.
DISPLAY CLRSCR.
OPEN OUTPUT GRDFILE.
WRITE GRDREC FROM HDG.
WRITE GRDREC FROM COLHDG.
PERFORM PROCESS-RTN THRU PROCESS-END
UNTIL ANS = 'N' OR ANS = 'n'.
CLOSE GRDFILE.
STOP RUN.
PROCESS-RTN.
DISPLAY (5, 15) "Enter Name: ".
ACCEPT (5, 30) NAME.
DISPLAY (7, 15) "Enter Midterm Grade: ".
ACCEPT (7, 40) MIDGRD.
DISPLAY (9, 15) "Enter Final Grade: ".
ACCEPT (9, 40) FINGRD.
COMPUTE AVE = (MIDGRD + FINGRD) / 2.
MOVE NAME TO NAME-OUT.
MOVE AVE TO AVE-OUT.
DISPLAY (11, 15) "Average Grade is: ", AVE-OUT.
WRITE GRDREC FROM GRDDATA .
DISPLAY (15, 15) "ENTER ANOTHER [Y/N]? ".
ACCEPT ANS.
PROCESS-END.
The problem I have is that when i open the grdfile, it shows random characters like cross and chinese characters.
if you have any idea, please do help. I want to learn. TIA
Seems to work perfectly well for me.
You should note that the size of HDG is 32+25+33=90, of COLHDG is 14+12+28+12=66 and GRDDATA is 14+25+20+4=63.
It may be that the compiler you are using is outputting random data on those short records as the output records are of length 80. I'd pad the short records out to 80 with filler pic x(14) value spaces and pic x(17) for the second and see whether that cures the problem.
Remember, the output will appear to be one giant string as far as a text editor is concerned...

Resources