user13829429
user13829429

Reputation:

Condition: positive and negative number in RPG

When I enter a negative number in my console, I don't get the display ! For example if enter the value ´-2´, I don't get the message 'negative`

In positive and null c'est ok

enter image description here

 H
 D Number          S              2S 0

  *
  /Free

      dsply 'Enter your number please : ' '' Number;

      If (Number > 0);
         dsply 'Positive';

      ElseIf (Number = 0);
         dsply 'Null';

      Else;
         dsply 'Negative';
      ENDIF;

      *inlr = *on;

  /End-Free

Upvotes: 0

Views: 1065

Answers (3)

WarrenT
WarrenT

Reputation: 4542

You can use %dec() to convert a numeric value in a string to a numeric variable. That will allow your users to place the negative sign on the left or the right. This might be a friendlier way of using the DSPLY opcode.

**FREE
   //simple demo, not worrying about exceptions

   dcl-s myStr  varchar(5);
   dcl-s myNum  packed(3:0);


   dsply 'Enter your number please : ' '' myStr;
   myNum = %dec(myStr:3:0);

   Select;
   When (myNum > 0);
     dsply 'Positive';

   When (myNum = 0);
     dsply 'Zero';     // not the same a null ;-)

   Other;
     dsply 'Negative';
   EndSl;

   *inlr = *on;
   RETURN;

Upvotes: 1

Barbara Morris
Barbara Morris

Reputation: 3674

If you do F1 (help) on the DSPLY message, it shows this:

Message ID . . . . . . :   RNQ5337       Severity . . . . . . . :   00         
Message type . . . . . :   Sender copy                                         
Date sent  . . . . . . :   12/18/20      Time sent  . . . . . . :   12:42:16   
                                                                               
Message . . . . :   DSPLY  Enter your number please :      0                   
Cause . . . . . :   This is an inquiry message originated from RPG procedure   
  MY_PGM in program MY_LIB/MY_PGM. The program is expecting a numeric 
  input field with a maximum length of 2 digits with 0 decimal positions.  Do  
  not type in a decimal point when entering data. When entering negative data, 
  type a negative sign ('-') immediately after the last digit of the data.     

Upvotes: 1

Scott Mildenberger
Scott Mildenberger

Reputation: 1607

In the RPGLE reference for the DSPLY op-code I found this excerpt:

If a non-float numeric field is entered with a length greater than the number of digits in the result field and the rightmost character is not a minus sign (-), an error is detected and a second wait occurs. The user must key in the field again.

It seems the DSPLY op-code expects the minus sign to be trailing and not leading.

Upvotes: 2

Related Questions