-2

I am creating a program that serves as a temperature converter (CELSIUS TO KELVIN and VICE VERSA). But I'm having trouble with my codes (or maybe there's a problem in the compiler. Idk). At first, I thought that I was using a wrong formula. But as I checked the input that's being stored in a variable by displaying it, it's just showing only one digit of the input. For example.. My input was 15, it only takes '1'.. Or let's say 45, it only takes '4'.. I declared the input like this:

01 CELSIUS PIC S9(2)V99. 

With my examples above, it gives me: 01.00 or 04.00 Please help.

 IDENTIFICATION DIVISION. ------------------ 
 PROGRAM-ID. temp. 
 ENVIRONMENT DIVISION. ------------------ 
 CONFIGURATION SECTION. 
 *----------------------- 
 INPUT-OUTPUT SECTION. *----------------------- 
 DATA DIVISION. ------------------ 
 FILE SECTION. *----------------------- 
 WORKING-STORAGE SECTION.

 01    TEMP-CELSIUS    PIC S9(2)V99.
 01    CELSIUS-RESULT    PIC +ZZ9.99.
 01    TEMP-KELVIN    PIC S9(2)V99.
 01    KELVIN-RESULT    PIC +ZZ9.99.
 01    VAR-OPTION    PIC X.
 01    X            PIC S9(5)V99.
*-----------------------
 PROCEDURE DIVISION.
*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-
 MAIN-PROCEDURE.
**
* The main procedure of the program
**

    DISPLAY 'OPTION 1: CELSIUS TO KELVIN'
    DISPLAY 'OPTION 2: KELVIN TO CELSIUS'
    DISPLAY 'ENTER YOUR OPTION: '
    ACCEPT VAR-OPTION

    MOVE +273.15 TO X
    IF VAR-OPTION = '1'
    DISPLAY 'ENTER CELSIUS:      '
        ACCEPT TEMP-CELSIUS
        DISPLAY TEMP-CELSIUS
       ADD X TEMP-CELSIUS GIVING KELVIN-RESULT
        DISPLAY KELVIN-RESULT
    ELSE
    IF VAR-OPTION = '2'
        DISPLAY 'ENTER KELVIN:       '
        ACCEPT TEMP-KELVIN
        DISPLAY TEMP-KELVIN
        SUBTRACT X FROM TEMP-KELVIN GIVING CELSIUS-RESULT
        DISPLAY CELSIUS-RESULT

   END-IF
   STOP RUN.
** add other procedures here
END PROGRAM temp.
Bill Woodger
  • 12,968
  • 4
  • 38
  • 47
sab
  • 11
  • 1
  • How do you expect people to debug your code when you don't show it to them? You need to post the _exact_ code which produces these problems so that we can reproduce them. – takendarkk Dec 06 '14 at 03:55
  • It is important to know which compiler you are using (OS may also be helpful). – Bill Woodger Dec 06 '14 at 08:29

1 Answers1

1

ACCEPT and DISPLAY are the COBOL verbs with the most wide-ranging non-standard implementations by compiler writers (for good reason, being that those writers wanted native COBOL to take screen input from users and provide screen output).

With Gnu COBOL 2.0, your program does not have the fault that you are looking for. I'd think the same with several compilers from Micro Focus.

From your description of the DISPLAY output (with a decimal-point) I'm surprised your code isn't working to that point. But we need to know the compiler. If you are showing the actual code, and have no hidden compiler options/swtiches, then my guess would be to remove the S (the implicit sign) from anything you ACCEPT.

A few other comments.

You seem to regard the first part of a program as "main", like in some other languages. Forget that idea, "main" is a very different thing which COBOL does not have.

If you have a label (a procedure name) like MAIN-PROCEDURE don't have a comment which says "this is the main procedure".

Why call anything X? Use good, descriptive names.

Look at using 88-level condition-names for your 1/2 test. Look to use EVALUATE rather than the nested-IF. You may not have done these things yet, use them if you have, look them up if you haven't so you'll better know them when they are introduced.

Why call something VAR-? Calling something VAR- when it can only be a field (variable) is a waste of space for a good name.

For the part of your program which is wrong (which you don't know about yet) always be aware that COBOL has fixed-length fields, you tell COBOL the length, so the length you tell it always has to be a correct size. I cannot convert the boiling-point of water under either scale, and can only convert the freezing-point from Celsius to Kelvin.

Bill Woodger
  • 12,968
  • 4
  • 38
  • 47
  • Thank you for the detailed answer and some suggestions. But I have found out that it's the compatibility in IDE versions that I've been using. I tried to download another version and it worked! Thanks for your help. Really appreciated! :) – sab Dec 07 '14 at 03:27
  • Can you write up your own Answer to the question, identifying the problem and the resolution. I may help others with a similar problem. Make sure you name the compiler, IDE and OS please. – Bill Woodger Dec 07 '14 at 12:11