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

Need help with my menu code.

Started by oneupe1even, 25 October 2012 - 10:55 AM
oneupe1even #1
Posted 25 October 2012 - 12:55 PM
So I made this menu for my cafe on a server, this is the first thing that I have coded in Lua. But I keep on getting an error I don't understand, and even the CC pro on my server couldn't fix it. Could you guys possibly help me? Here is my code:


term.clear()
term.setCursorPos(1,1)

print("Welcome to El Cafe Uno!")
print("Here are all of our wonderful products that we offer!")
print("Please type the name of the item to view information and prices.")

term.setCursorPos(1,5)
print("Drinks:")
term.setCursorPos(1,7)
print("Coffee")
print("Milk")
print("Juice")
print("Water")
print("Hot Water")
print("Rum")
print("Beer")

term.setCursorPos(1,14)

print("Food:")
term.setCursorPos(1,18)
print("Cookies")
print("Bread")
print("Honey Bread")
term.setCursorPos(1,22)
print("Please input an item from our menu above:")
term.setCursorPos(1,24)
input = read()

if input == "Coffee" then
term.clear()
term.setCursorPos(1,1)
print("Our coffee is brewed daily in our brewery downstairs, our coffee beans are grown with love.")
print("Price: 2 Iron Nuggets")
sleep(10)
term.clear()
term.setCursorPos(1,1)
shell.run("startup")

else if input == "Milk" then
term.clear()
term.setCursorPos(1,1)
print("We get our milk fresh from cows trapped in the basement, with love of course!")
print("Price: 2 Iron Nuggets")
sleep(10)
term.clear()
term.setCursorPos(1,1)
shell.run("startup")

else if input == "Juice" then
term.clear()
term.setCursorPos(1,1)
print("Our juice is made fresh daily. We use apples from tree farms, mexican plantations, and trashcans!")
print("Price: 4 Iron Nuggets")
sleep(10)
term.clear()
term.setCursorPos(1,1)
shell.run("startup")

else if input == "Water" then
term.clear()
term.setCursorPos(1,1)
print("It's water, what could go wrong?")
print("Price: 1 Iron Nugget")
sleep(10)
term.clear()
term.setCursorPos(1,1)
shell.run("startup")

else if input == "Hot Water" then
term.clear()
term.setCursorPos(1,1)
print("It's actually just lava from underground pools, but you don't need to know that.")
print("Price: 4 Iron Nuggets")
sleep(10)
term.clear()
term.setCursorPos(1,1)
shell.run("startup")

else if input == "Rum" then
term.clear()
term.setCursorPos(1,1)
print("Yarr Matey! Captain Porkchop gives our rum two hooks up!")
print("Price: 5 Iron Nuggets")
sleep(10)
term.clear()
term.setCursorPos(1,1)
shell.run("startup")

else if input == "Beer" then
term.clear()
term.setCursorPos(1,1)
print("It gets you drunk!")
print("Price: 8 Iron Nuggets")
sleep(10)
term.clear()
term.setCursorPos(1,1)
shell.run("startup")

else if input == "Cookies" then
term.clear()
term.setCursorPos(1,1)
print("These individual cakes of yum are perfect with coffee.")
print("Price: 1 Iron Nugget")
sleep(10)
term.clear()
term.setCursorPos(1,1)
shell.run("startup")

else if input == "Bread" then
term.clear()
term.setCursorPos(1,1)
print("It's your standard, run of the mill bread.")
print("Price: 2 Iron Nuggets")
sleep(10)
term.clear()
term.setCursorPos(1,1)
shell.run("startup")

else if input == "Honey Bread" then
term.clear()
term.setCursorPos(1,1)
print("Dis some fancy shit right here.")
print("Price: 10 Iron Nuggets")
sleep(10)
term.clear()
term.setCursorPos(1,1)
shell.run("startup")

else
print("Please check your spelling and try again.")
sleep(1)
print("Reloading menu....")
sleep(4)
shell.run("startup")
end

The error I'm getting is:

bios:206: [string "startup"]:137: 'end' expected (to close 'if' at line 111)
Kingdaro #2
Posted 25 October 2012 - 12:57 PM
Try writing "elseif" instead of "else if"?
remiX #3
Posted 25 October 2012 - 12:59 PM
How could your so called 'CC pro' on your server not fix that O.o
oneupe1even #4
Posted 25 October 2012 - 01:24 PM
Try writing "elseif" instead of "else if"?

Thanks, that fixed it.
remiX #5
Posted 25 October 2012 - 03:32 PM
Haha everyone makes silly mistake. If you want a shorter code you could use tables.
I had nothing to do so I did it for you, it's also easy to edit.
Spoiler

drinks = {"Coffee","Milk","Juice","Water","Hot Water","Rum","Beer"}
food = {"Cookies","Bread","Honey Bread"}
descriptions = {
["Coffee"] = "Our coffee is brewed daily in our brewery downstairs, our coffee beans are grown with love.nPrice: 2 Iron Nuggets",
["Milk"] = "We get our milk fresh from cows trapped in the basement, with love of course!nPrice: 2 Iron Nuggets",
["Juice"] = "Our juice is made fresh daily. We use apples from tree farms, mexican plantations, and trashcans!nPrice: 4 Iron Nuggets",
["Water"] = "It's water, what could go wrong?nPrice: 1 Iron Nugget.",
["Hot Water"] = "It's actually just lava from underground pools, but you don't need to know that.nPrice: 4 Iron Nuggets",
["Rum"] = "Yarr Matey! Captain Porkchop gives our rum two hooks up!nPrice: 5 Iron Nuggets",
["Beer"] = "It gets you drunk!nPrice: 8 Iron Nuggets",
["Cookies"] = "These individual cakes of yum are perfect with coffee.nPrice: 1 Iron Nuggets",
["Bread"] = "It's your standard, run of the mill bread.nPrice: 2 Iron Nuggets",
["Honey Bread"] = "Dis some fancy shit right here.nPrice: 10 Iron Nuggets"
}
foodChoice = nil

function goodPrint(s,o,x,y,c)
    if c == "c" then term.clear() end
    if x == 0 then local cx = term.getSize() x = ( cx - string.len(s) ) / 2 end
    term.setCursorPos(x,y)
    if o == "p" then print(s) elseif o == "w" then write(s) end
end
while true do
    goodPrint("Welcome to El Cafe Uno!", "p", 0, 1, "c")
    goodPrint("Here are all of our wonderful products", "p", 0, 2)
    goodPrint("that we offer!","p",0,3)
    goodPrint("Please type the name of the item to view", "p", 0, 4)
    goodPrint("information and prices.","p",0,5)
    goodPrint("Drinks:","p",10,7)
    goodPrint("Food:","p",28,7)

    for i = 1, 7 do
        term.setCursorPos(10,i+8)
        print(drinks[i])
    end
    for i = 1, 4 do
        term.setCursorPos(28,i+8)
        print(food[i])
    end

    goodPrint("Please input an item from our menu above:n  > ", "w", 1, 17)
    input = read()

    for i = 1, 7 do
        if drinks[i] == input or food[i] == input then
            Choice = input
            if Choice ~= nil then goodPrint(descriptions[Choice],"p",1,1,"c") sleep(3) break end
            break
        end
        if i == 7 then goodPrint("Please check your spelling and try again","p",1,1,"c") sleep(1) print("Reloading menu...") sleep(4) end
    end
end