Make numbers limited to 0-359 as input or output

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Make numbers limited to 0-359 as input or output

Bill Kelsoe
Hi, I recently built a sample code for calculating reciprocal winds for flight simulation purposes. I want to limit user input to numbers from 0-359 inclusive. Nothing like 360 should be entered by user. How can I do this?

local line
repeat
print("Input wind heading: ");
line = io.read()     -- read a line
    n = tonumber(line)   -- try to convert it to a number
    k = tonumber(line)
    k = 180-n
    if n > 180 then
      print("Reciprocal heading is: ",-k)
    elseif n < 180 then
      print("Reciprocal heading is: ", n+180) 
      else n = 180
      print ("are you kidding me?")
    end  
    print("do you want to convert again? y/n?")
    line = io.read()
until line ~= "y"
print("Goodbye")

Also how can I input this into the source code executable to run it? If I press enter after the first line it just stops at local line.

Thanks in advance
Adel
Reply | Threaded
Open this post in threaded view
|

Re: Make numbers limited to 0-359 as input or output

dyngeccetor8
On 07/20/2018 07:50 AM, Bill Kelsoe wrote:

> Hi, I recently built a sample code for calculating reciprocal winds for
> flight simulation purposes. I want to limit user input to numbers from
> 0-359 inclusive. Nothing like 360 should be entered by user. How can I do
> this?
>
> local line
> repeat
> print("Input wind heading: ");
> line = io.read()     -- read a line
>     n = tonumber(line)   -- try to convert it to a number
>     k = tonumber(line)
>     k = 180-n
>     if n > 180 then
>       print("Reciprocal heading is: ",-k)
>     elseif n < 180 then
>       print("Reciprocal heading is: ", n+180)
>       else n = 180
>       print ("are you kidding me?")
>     end
>     print("do you want to convert again? y/n?")
>     line = io.read()
> until line ~= "y"
> print("Goodbye")
>
> Also how can I input this into the source code executable to run it? If I
> press enter after the first line it just stops at local line.
>
> Thanks in advance
> Adel

local answer = 'y'
while (answer == 'y') do
  io.write('Input wind heading: ')
  local s = io.read()

  local wind_direction
  local n = tonumber(s)
  if
    not (n and (n >= 0) and (n <= 359) and (math.type(n) == 'integer'))
  then
    print('are you kidding me?')
  else
    wind_direction = n
  end

  if wind_direction then
    -- Rotate by 180 deg:
    wind_direction = (wind_direction + 180) % 360
    print(('Reciprocal heading is: %d'):format(wind_direction))
  end

  io.write('do you want to convert again? y/n?: ')
  answer = io.read()
end

-- Martin

Reply | Threaded
Open this post in threaded view
|

Re: Make numbers limited to 0-359 as input or output

Sam Putman
In reply to this post by Bill Kelsoe


On Fri, Jul 20, 2018 at 6:50 AM, Bill Kelsoe <[hidden email]> wrote:
Hi, I recently built a sample code for calculating reciprocal winds for flight simulation purposes. I want to limit user input to numbers from 0-359 inclusive. Nothing like 360 should be entered by user. How can I do this?


You can certainly do this, but do you need to?

-30 and 720 both have unambiguous interpretations as degrees.

Even if you don't want to support negative degrees (admittedly a bit more complex), a simple n % 360 would give 
accurate results,  which shouldn't be surprising to your users.
Reply | Threaded
Open this post in threaded view
|

Re: Make numbers limited to 0-359 as input or output

Bill Kelsoe
It must not display values above 359 for results too. 

On Fri, Jul 20, 2018 at 13:39 Sam Putman <[hidden email]> wrote:
On Fri, Jul 20, 2018 at 6:50 AM, Bill Kelsoe <[hidden email]> wrote:
Hi, I recently built a sample code for calculating reciprocal winds for flight simulation purposes. I want to limit user input to numbers from 0-359 inclusive. Nothing like 360 should be entered by user. How can I do this?


You can certainly do this, but do you need to?

-30 and 720 both have unambiguous interpretations as degrees.

Even if you don't want to support negative degrees (admittedly a bit more complex), a simple n % 360 would give 
accurate results,  which shouldn't be surprising to your users.