I think there is something wrong with my computation part in my code, I cant get a decimal output for my currency converter

140 Views Asked by At
 IDENTIFICATION DIVISION.        
 PROGRAM-ID. MP2.    
 ENVIRONMENT DIVISION.  
 DATA DIVISION.  
 WORKING-STORAGE SECTION.  
 01 AMOUNT PIC 9(4)V9(4).  
 01 AMTDIV PIC 99V9(4).  
 01 CURR    PIC X(3).  
    88 PHP VALUE "PHP" "php".  
    88 USA VALUE "USD" "usd".  
    88 CND VALUE "CAD" "cad".  
    88 AUS VALUE "AUD" "aud".   
 01 RECURR  PIC X(3).         
    88 PHPK VALUE "PHP" "php".           
    88 USAK VALUE "USD" "usd".   
    88 CNDK VALUE "CAD" "cad".   
    88 AUSK VALUE "AUD" "aud".      
 01 CONFIRM PIC X.          
    88 AGREE VALUE "Y" "y".     
    88 DISAGREE VALUE "N" "n".    



 PROCEDURE DIVISION.   
 START-UP.   
 DISPLAY "CURRENCY LIST".    
 DISPLAY "USD (US DOLLARS), CAD (CANADIAN DOLLAR)".   
 DISPLAY "PHP (PHILIPPINE PESO), AUD (AUSTRALIAN DOLLAR)".   
 DISPLAY " ".       
 MAIN-ROUTINE.     
 DISPLAY "ENTER AMOUNT: ".   
 ACCEPT AMOUNT.   
 DISPLAY "ENTER SOURCE CURRENCY: "     
 ACCEPT CURR.      
 IF PHP    
    DISPLAY "PHILLIPINE PESO"    
    DISPLAY "ENTER TARGET CURRENCY: "     
    ACCEPT RECURR     
    IF USAK      
    DISPLAY "AMERICAN DOLLAR"      
    MOVE 435450 TO AMTDIV     
    ELSE IF CNDK     
    DISPLAY "CANADIAN DOLLAR"     
    MOVE 416707 TO AMTDIV      
    ELSE IF AUSK      
    DISPLAY "AUSTRALIAN DOLLAR"      
    MOVE 410325 TO AMTDIV       
    ELSE     
    DISPLAY "INVALID OPTION"     
 ELSE IF USA     
    DISPLAY "AMERICAN DOLLAR"     
    DISPLAY "ENTER TARGET CURRENCY: "     
    ACCEPT RECURR     
    IF PHPK     
    DISPLAY "PHILIPPINE PESO"     
    MOVE 000230 TO AMTDIV     
    ELSE IF CNDK     
    DISPLAY "CANADIAN DOLLAR"     
    MOVE 009574 TO AMTDIV      
    ELSE IF AUSK      
    DISPLAY "AUSTRALIAN DOLLAR"      
    MOVE 009423 TO AMTDIV       
    ELSE      
    DISPLAY "INVALID OPTION"     
 ELSE IF CND       
    DISPLAY "CANADIAN DOLLAR"     
    DISPLAY "ENTER TARGET CURRENCY: "     
    ACCEPT RECURR     
    IF PHPK     
    DISPLAY "PHILIPPINE PESO"     
    MOVE 000240 TO AMTDIV     
    ELSE IF USAK     
    DISPLAY "AMERICAN DOLLAR"      
    MOVE 010442 TO AMTDIV      
    ELSE IF AUSK      
    DISPLAY "AUSTRALIAN DOLLAR"      
    MOVE 009837 TO AMTDIV       
    ELSE     
    DISPLAY "INVALID OPTION"      

 ELSE IF AUS        
    DISPLAY "AUSTRALIAN DOLLAR"     
    DISPLAY "ENTER TARGET CURRENCY: "     
    ACCEPT RECURR     
    IF PHPK      
    DISPLAY "PHILIPPINE PESO"      
    MOVE 000244 TO AMTDIV      
    ELSE IF CNDK      
    DISPLAY "CANADIAN DOLLAR"      
    MOVE 010166 TO AMTDIV     
    ELSE IF USAK     
    DISPLAY "AMERICAN DOLLAR"     
    MOVE 010612 TO AMTDIV       
    ELSE     
    DISPLAY "INVALID OPTION"            
 ELSE     
    DISPLAY "INVALID OPTION".        



 DIVIDE AMOUNT BY AMTDIV GIVING AMOUNT.    

 DISPLAY "CONVERTED AMOUNT: "AMOUNT.     


 ONE-MORE-TRY.    
 DISPLAY "WOULD YOU LIKE TO CONVERT ANOTHER CURRENCY? [Y/N]?".    
 ACCEPT CONFIRM.     
 IF AGREE    
    PERFORM MAIN-ROUTINE THRU ONE-MORE-TRY     
 ELSE IF DISAGREE     
    PERFORM END-PGM    
 ELSE    
    DISPLAY "INVALID OPTION"     
    PERFORM ONE-MORE-TRY.    
 END-PGM.    
 STOP RUN.
2

There are 2 best solutions below

2
On

Try putting the decimal point into your constants. COBOL is smart enough to align the decimal with the V (virtual decimal point) and maps the storage method provided by the PICTURE so that relatively-efficient sdata storage can be translated back-and-forth to human-readable form.

Never use PERFORM THROUGH - it produces layout-dependent code.

Your PERFORM MAIN-ROUTINE THRU ONE-MORE-TRY should be GO TO PERFORM MAIN-ROUTINE

PERFORM END-PGM should be GO TO END-PGM

and PERFORM ONE-MORE-TRY. should be GO TO ONE-MORE-TRY.

A PERFORM executes the code starting at the target label as a subroutine, so your current structure is endlessly building a return-address stack until you bail out via the STOP RUN.

0
On

You have two problems in getting your DIVIDE to work.

In order of occurrence:

You ACCEPT your AMOUNT. Your AMOUNT has an implied decimal place (the V in the PICture string), yet ACCEPT is going to, for your purpose, ignore this implied decimal. There will be no alignment with what the user types at the screen. There is more than one way to deal with this, perhaps the simplest for your purpose it to look at the Intrinsic FUNCTION NUMVAL.

You, as @Magoo indicated, do not use decimal points in your literals, so they are treated as whole numbers, so effectively the figures you expect to be used for the currency conversion are multiplied by 10,000 and left-truncated.

When reporting a problem it is a good idea to show the input data which gave you the problem, the result you actually achieved, and the expected result. If you can work out what is happening but are unsure how to correct it, that is a bonus.

You have tagged Coding-Style. I think you might want to remove that by editing your question. The people interested in the Coding-Style tag are probably not too aware of COBOL. If you have a future Career as a COBOL programmer, your style is going to be more or less dictated by the site standards of where you work, and will change from site to site. You can, of course, develop your own style, but it develops, it isn't just given to you. Knowing the messes you can get into helps you develop style (technique) in avoiding getting there. You'll still need to know how things happen, because not all programmers take the time to develop anything in the way of style.

Read through some of the questions here, and see if you get some ideas about common problems.