Validation of data entry in REXX

41 Views Asked by At

How to enter only integers in REXX. The program I have manages to avoid some entries, but when the number contains a character such as a comma, percentage, cipher, among others, it causes the program to stop. How to validate these operations and let the program operate only with integer input.

do forever
  call charout, "Enter number: "
  pull NumInt
  say

  if (NumInt\== '') then 
    do
      if (datatype(value(NumInt), 'W')) then 
        leave
      else 
      do
        say "Error: Enter only whole number."
        say
      end
    end
  else
    do
      say "Error: <Enter> key accidentally triggered."
      say
    end
end

say "Valid input provided: " || NumeroInteiro

say
call charout, "Press <Enter> to finish... "
pull

exit
2

There are 2 best solutions below

1
Augusto Manzano On BEST ANSWER

Based on the structure of your program, I believe you want something like:

charSpecial: procedure
arg String
Char = "!@#$%^&*()-_=+[]{}|;:',<>/?`~" || '"'
do i = 1 to length(Char)
  if pos(substr(Char, i, 1), String) > 0 then 
    return 1
end
return 0

See the use of this in the code:

do forever
  call charout, "Enter number: "
  pull NumInt
  say

  if (NumInt \== '') then 
    do
      if (charSpecial(NumInt)) then 
        do
          say "Error: Special character not allowed."
          say
        end
      else 
        if (datatype(value(NumInt), 'W')) then 
          leave
        else 
          do
            say "Error: Enter only whole number."
            say
          end
    end
  else
    do
      say "Error: <Enter> key accidentally triggered."
      say
    end
end

say "Valid input provided: " || NumInt

say
call charout, "Press <Enter> to finish... "
pull

exit
0
William Downie On

What I think you are wanting to achieve is allow some characters that are not numeric....how about removing all characters that are valid, but not numeric, before checking the datatype, for example (I'm assuming a space is valid ) :

translatetable = ",.%$£-"       /* valid characters */
if (NumInt\== '') then
  x = space(translate(NumInt,' ',translatetable),0) 
  if (datatype(value(x), 'W')) then
    etc etc