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

Need help with a simple program (got a bug)

Started by Shehab19, 21 October 2014 - 05:47 PM
Shehab19 #1
Posted 21 October 2014 - 07:47 PM
so basically I'm trying to make a cash register of some sort. this is the code:

– Items on sale + GUI
term.clear()
term.setCursorPos(18,1)
write("PRICE CALCULATOR")
term.setCursorPos(1,2)
write("—————————————————")
term.setCursorPos(2,3)
write("Items on sale: :")
term.setCursorPos(1,4)
print("1) Computer :")
print("2) Turtle :")
print("3) Monitor :")
print("4) Printer :")
print("5) Modem :")
print("6) Mine/fell Turtle :")
print("7) Fight/Shov Turtle :")
print("8) Farming Turtle :")
print("9) Advanced Comp :")
print("10) Advanced Mon :")



– Choice 1
term.setCursorPos(24,3)
print("Pick Items By Number.")
term.setCursorPos(24,4)
item = read()
if item == ("1"or"2"or"3"or"4"or"5"or"6"or"7"or"8"or"9") then
term.setCursorPos(24,4)
print("Quantity:")
term.setCursorPos(33,4)
end
if item ~= ("1"or"2"or"3"or"4"or"5"or"6"or"7"or"8"or"9") then
term.setCursorPos(27,4)
print("Invalid Option.")
sleep(1)
os.reboot()
end
– quantity
quantity = read()
if quantity == "1"or"2"or"3"or"4"or"5"or"6"or"7"or"8"or"9"or"10"
then term.setCursorPos(24,5)
print("Anything else? yes/no")
end
if quantity ~= "1"or"2"or"3"or"4"or"5"or"6"or"7"or"8"or"9"or"10"
then term.setCursorPos(33,4)
print("Invalid Option")
sleep(1)
os.reboot()
end








The problem is when i input "1" in the first step to select the computer, it brings up the next step with is the quantity but the only answer that works for quantity is the number "1". anything else will display incorrect option as well as anything else yes/no. can anyone help me fix this error? i cant continue the code for the other items without this bug being fixed! (if you can find a way to shorten my code that would be a huge help too!), (the final product should be able to pick a number of products with each having a price and adding them to have a final price and knowing what you owe people. thought i would share the idea :D/>)
KingofGamesYami #2
Posted 21 October 2014 - 08:09 PM
You are using 'or' incorrectly.

if a == "1" or a == "2" or a == "3" ... then
I would use tonumber and math.floor for this anyway.

local item
repeat
  item = math.floor( tonumber( read() ) )
until item and item >= 1 and item <= 10
Edited on 21 October 2014 - 06:09 PM
Xenthera #3
Posted 21 October 2014 - 08:10 PM
Eh.. heh. ermmm.. Well… I suggest learning lua a little better. Also, as a side note, post questions to Ask A Pro. The programs section is for completed programs for people to share.

As a little mini lesson,


item = read()
if item == ("1"or"2"or"3"or"4"or"5"or"6"or"7"or"8"or"9") then
term.setCursorPos(24,4)
print("Quantity:")
term.setCursorPos(33,4)
end
if item ~= ("1"or"2"or"3"or"4"or"5"or"6"or"7"or"8"or"9") then
term.setCursorPos(27,4)
print("Invalid Option.")
sleep(1)
os.reboot()
end

can be shortened to


item = tonumber(read()) --# Turn the string into a number (implicitly inferred int double or float) if possible
if item >= 1 and item <= 9 then --# Check if it's in range of your options. IF you were using a table you'd use something like if item >= 1 and item <= #table
  term.setCursorPos(24,4)
  print("Quantity:")
  term.setCursorPos(33,4)
else --# Instead of rewriting an if statement, just catch every other input
  term.setCursorPos(27,4)
  print("Invalid Option.")
end

Also, use a while loop so you don't have to restart the computer every time someone enters an invalid option.

The same goes for the quantity. No need to check things more than you have to.
Edited on 21 October 2014 - 06:16 PM
Shehab19 #4
Posted 21 October 2014 - 08:23 PM
thanks guys! this code probably made you wanna gouge your eyes out but im pretty new lol. ty for the help!
Shehab19 #5
Posted 21 October 2014 - 08:34 PM
Eh.. heh. ermmm.. Well… I suggest learning lua a little better. Also, as a side note, post questions to Ask A Pro. The programs section is for completed programs for people to share.

As a little mini lesson,


item = read()
if item == ("1"or"2"or"3"or"4"or"5"or"6"or"7"or"8"or"9") then
term.setCursorPos(24,4)
print("Quantity:")
term.setCursorPos(33,4)
end
if item ~= ("1"or"2"or"3"or"4"or"5"or"6"or"7"or"8"or"9") then
term.setCursorPos(27,4)
print("Invalid Option.")
sleep(1)
os.reboot()
end

can be shortened to


item = tonumber(read()) --# Turn the string into a number (implicitly inferred int double or float) if possible
if item >= 1 and item <= 9 then --# Check if it's in range of your options. IF you were using a table you'd use something like if item >= 1 and item <= #table
  term.setCursorPos(24,4)
  print("Quantity:")
  term.setCursorPos(33,4)
else --# Instead of rewriting an if statement, just catch every other input
  term.setCursorPos(27,4)
  print("Invalid Option.")
end

Also, use a while loop so you don't have to restart the computer every time someone enters an invalid option.

The same goes for the quantity. No need to check things more than you have to.
so i applied your helpful tips to the code and it fixed most problems until i hit soemthing else.


– Items on sale + GUI
term.clear()
term.setCursorPos(18,1)
write("PRICE CALCULATOR")
term.setCursorPos(1,2)
write("—————————————————")
term.setCursorPos(2,3)
write("Items on sale: :")
term.setCursorPos(1,4)
print("1) Computer :")
print("2) Turtle :")
print("3) Monitor :")
print("4) Printer :")
print("5) Modem :")
print("6) Mine/fell Turtle :")
print("7) Fight/Shov Turtle :")
print("8) Farming Turtle :")
print("9) Advanced Comp :")
print("10) Advanced Mon :")



– Choice 1
term.setCursorPos(24,3)
print("Pick Items By Number.")
term.setCursorPos(24,4)
item = tonumber(read())
if item >= 1 and item <= 9 then
term.setCursorPos(24,4)
print("Quantity:")
term.setCursorPos(33,4)
else
term.setCursorPos(27,4)
print("Invalid Option.")
end
– quantity
quantity =tonumber(read())
if quantity >= 1 and item <= 9 then
term.setCursorPos(24,5)
print("Anything else? yes/no")
end
else term.setCursorPos(33,4)
print("Invalid Option")
sleep(1)
os.reboot()
end

line 42 has an eof expected. i got no clue how to fix this any pointers?
KingofGamesYami #6
Posted 21 October 2014 - 08:40 PM

-- Items on sale + GUI
term.clear()
term.setCursorPos(18,1)
write("PRICE CALCULATOR")
term.setCursorPos(1,2)
write("---------------------------------------------------")
term.setCursorPos(2,3)
write("Items on sale: :")
term.setCursorPos(1,4)
print("1) Computer :")
print("2) Turtle :")
print("3) Monitor :")
print("4) Printer :")
print("5) Modem :")
print("6) Mine/fell Turtle :")
print("7) Fight/Shov Turtle :")
print("8) Farming Turtle :")
print("9) Advanced Comp :")
print("10) Advanced Mon :")



-- Choice 1
term.setCursorPos(24,3)
print("Pick Items By Number.")
term.setCursorPos(24,4)
item = tonumber(read()) 
if item >= 1 and item <= 9 then
  term.setCursorPos(24,4)
  print("Quantity:")
  term.setCursorPos(33,4)
else
  term.setCursorPos(27,4)
  print("Invalid Option.")
end
-- quantity
quantity =tonumber(read())
if quantity >= 1 and item <= 9 then  --#FYI, if a non-number is entered this will error!
  term.setCursorPos(24,5)
  print("Anything else? yes/no")
end --#ah, indentation reveals the problem...
else
  term.setCursorPos(33,4)
  print("Invalid Option")
  sleep(1)
  os.reboot()
end
Edited on 21 October 2014 - 06:41 PM
Shehab19 #7
Posted 21 October 2014 - 08:46 PM

-- Items on sale + GUI
term.clear()
term.setCursorPos(18,1)
write("PRICE CALCULATOR")
term.setCursorPos(1,2)
write("---------------------------------------------------")
term.setCursorPos(2,3)
write("Items on sale: :")
term.setCursorPos(1,4)
print("1) Computer :")
print("2) Turtle :")
print("3) Monitor :")
print("4) Printer :")
print("5) Modem :")
print("6) Mine/fell Turtle :")
print("7) Fight/Shov Turtle :")
print("8) Farming Turtle :")
print("9) Advanced Comp :")
print("10) Advanced Mon :")



-- Choice 1
term.setCursorPos(24,3)
print("Pick Items By Number.")
term.setCursorPos(24,4)
item = tonumber(read())
if item >= 1 and item <= 9 then
  term.setCursorPos(24,4)
  print("Quantity:")
  term.setCursorPos(33,4)
else
  term.setCursorPos(27,4)
  print("Invalid Option.")
end
-- quantity
quantity =tonumber(read())
if quantity >= 1 and item <= 9 then  --#FYI, if a non-number is entered this will error!
  term.setCursorPos(24,5)
  print("Anything else? yes/no")
end --#ah, indentation reveals the problem...
else
  term.setCursorPos(33,4)
  print("Invalid Option")
  sleep(1)
  os.reboot()
end
sorry to bug you with dumb questions but, i dont see any error and even with the code above exactly written the same error occurs :|
Dragon53535 #8
Posted 21 October 2014 - 08:49 PM
The code he put is the code you put however indented to show the error, that end he's pointing at is your problem. if else if's are written like this

if condition then
  --do stuff
elseif condition then --Notice the lack of end before this
  -- do other stuff
else --and before this!
  --do last stuff
end
Edited on 21 October 2014 - 06:50 PM
KingofGamesYami #9
Posted 21 October 2014 - 08:50 PM
You:

if <condition> then
  --#do stuff
end
else
  --#do stuff
end
Correct:

if <condition> then
  --#do stuff
else
  --#do stuff
end
Lyqyd #10
Posted 21 October 2014 - 09:43 PM
Moved to Ask a Pro.