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

Computer turns off after starting an program?

Started by Zambonie, 05 January 2013 - 12:03 PM
Zambonie #1
Posted 05 January 2013 - 01:03 PM
So,Im trying to make an Guess the number pro edition,and when I start the program,after the intro,the computer freezes and just turns off.Here is the code:


--To set off stuff
ver = ("v.1.0")
maxt = ("11")
tr = ("1")
win = ("false")
numb = math.random(1,1000)
--Intro
term.clear()
sleep(1)
term.setCursorPos(21,9)
textutils.slowPrint("P4 studios")
sleep(3)
term.clear()
--GTN Intro
term.setCursorPos(17,9)
textutils.slowPrint( [[ ------------------
				 Guess That Number!
				 ------------------ ]] )
				 term.setCursorPos(18,12)
				 sleep(1)
		  print("Pro e.	  "..ver)
				 term.setCursorPos(16,15)
				 sleep(1)
		  print("Press Enter To Continue!")
io.read(enter)
--Game
term.clear()
term.setCursorPos(1,1)
print("Welcome!Pick a number from 1-1000!")
print("You have 10 tries.")
while true do
if tr > maxt then
guess = io.read()
guess = tonumber(guess)
if guess == numb then
win = ("true")
else
print("You got this one wrong,But try again!")
if guess > numb then
print("Your guess was to high!")
else
if guess < numb then
print("Your guess was to low!")
else
print("Ok?Invalid input?")
end
end
end
end
end
--results
if win == true then
print("You won!")
end
if win == false then
print("You lost!")
end	   
Mainly were it sais "–Game"Is were it shuts off.So,Is there any thing im doing wrong?
KaBooMa #2
Posted 05 January 2013 - 01:17 PM
I am going to take a big shot in the dark and say that your
io.read(enter)
needs to be changed to
io.read("enter")

I haven't used io.read but that is what i would say. Try it :)/>

EDIT: Actually, It sorta looks like io.read reads a file. Trying using this code instead

local key = 0
repeat -- repeat until the key pulled is 28 (enter)
  local _, key = os.pullEvent("key")
until key == 28 -- 28 is the enter keycode

EDIT2: Also, it looks like you use io.read() to read for input a lot. Try doing just
local input = read()
Zambonie #3
Posted 05 January 2013 - 02:08 PM
Well,It worked lastime,(well not the 10 times thing,im working on that)and without editing anything,this pops up.It works when I press enter,the screen clears,and the text appears,and from that moment,until the text appears,it shuts off.
Zambonie #4
Posted 05 January 2013 - 02:12 PM
And,ok,so what code should I use and where do I replace it?
Leo Verto #5
Posted 05 January 2013 - 03:45 PM
Ok, I added proper indentation, try to do that yourself next time, it makes reading your code a lot easier.
I also cleaned up some ifs and fixed your io.read(), I changed it to allow any key because I am too tired to look up the number for enter right now.

Here's the code, it should work but tell me if you've got an error and what it is.
Spoiler

--To set off stuff
ver = "v.1.0"
maxt = "11"
tr = "1"
win = "false"
numb = math.random(1,1000)
--Intro
term.clear()
term.setCursorPos(21,9)
textutils.slowPrint("P4 studios")
sleep(3)
term.clear()
--GTN Intro
term.setCursorPos(17,9)
textutils.print(
[[ ------------------
Guess That Number!
------------------ ]])
term.setCursorPos(18,12)
sleep(1)
print("Pro e.   "..ver)
term.setCursorPos(16,15)
sleep(1)
print("Press any key To Continue!")
while true do
evt, p1 = os.pullEvent("key")
--Game
term.clear()
term.setCursorPos(1,1)
print("Welcome!Pick a number from 1-1000!")
print("You have 10 tries.")
while true do
  if tr > maxt then
    guess = io.read()
    guess = tonumber(guess)
    if guess == numb then
	  win = ("true")
    else
	  print("You got this one wrong,But try again!")
	  if guess > numb then
	    print("Your guess was to high!")
	  elseif guess < numb then
	    print("Your guess was to low!")
	  else
	    print("Ok? Invalid input?")
	  end
    end
  end
end
--results
if win == true then
  print("You won!")
else
  print("You lost!")
end 
KaBooMa #6
Posted 05 January 2013 - 04:26 PM
Sorry I wasn't on to reply…Go with what Leo Verto said and see if it works :)/> I personally have never used io.read so I can't really tell you if it is the problem. Kinda figured if it was doing everything but stopping at – Clear, that had to be the problem. I read that io.read does reading files so ya. Sorry if I was wrong :)/>