Jump to content




Computer turns off after starting an program?


5 replies to this topic

#1 Zambonie

  • Members
  • 477 posts
  • LocationSpring Hill, Fl

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?

#2 KaBooMa

  • New Members
  • 29 posts

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()


#3 Zambonie

  • Members
  • 477 posts
  • LocationSpring Hill, Fl

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.

#4 Zambonie

  • Members
  • 477 posts
  • LocationSpring Hill, Fl

Posted 05 January 2013 - 02:12 PM

And,ok,so what code should I use and where do I replace it?

#5 Leo Verto

  • Members
  • 620 posts
  • LocationOver there

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


#6 KaBooMa

  • New Members
  • 29 posts

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 :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users