This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
maximlus's profile picture

[Lua] problem with code.

Started by maximlus, 21 July 2012 - 03:53 PM
maximlus #1
Posted 21 July 2012 - 05:53 PM
I am trying to make a real world clock for my server.
Here is the code that I have written, for some reason when I run the program it will work and print the first print but then will freeze and I have to terminate it.
The idea is for it to loop indefinably.



hours = 17
minutes = 36
print (hours, ":", minutes)
while hours <= 25 do
while minutes < 60 do
minutes = minutes + 1
sleep (60)
end
if minutes = (60) then
hours = hours + 1
minutes = 0
end
if hours = (24) then
hours = 00
end
shell.run("clear")
print (hours, ":", minutes)
end


Thank you for taking the time to read this.
OmegaVest #2
Posted 21 July 2012 - 08:49 PM

term.clear()  -- Clears the screen
term.setCursorPos(1,1)  -- Sets the cursor back to default

term.write("Current Hour: ")  -- This whole block allows the user to set the time is actually is.
hours = tonumber(io.read())  --          ''                ''

term.write("n Current Minute: ")   --  ''                ''
minutes = tonumber(io.read())   --      ''                ''


while true do    --  Big while loop
   if minutes + 1 > 60 then    -- Check for hour change
      minutes = 0
      if hours + 1 > 24 then   -- Check for day change
         hours = 0
      else
         hours = hours + 1
      end
   else
      minutes = minutes + 1
   end

   term.clear()      -- This block just makes a good, clean UI that is not just "stuck in top corner".
   termx, termy = term.getSize()
   term.setCursorPos((termx/2)-3 , 3)
   term.write(hours)
   term.write(":")
   term.write(minutes)
   sleep(59.5)
end


And that should be timed right, as well. The problem you had is that you called your prints and ifs outside the loops. Also, you need to use == for comparisons, not =.