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

User input to strings?

Started by Zambonie, 23 December 2012 - 05:31 AM
Zambonie #1
Posted 23 December 2012 - 06:31 AM
So I am trying to make an calculater,and so far I got the screen that says "Calculater" and "Please enter your calculation".And my prob is that I want to make an user put in there own string.I am kinda new to lua,and this is the code im using for the user input string:

local input = read()
if input == ("..a.."+"..b..") then

Thats mainly waht I got.The "..a.." and "..b.." are the user uinput strings,but the computer is not just catching the numbers im putting in.So does anyone know what I did wrong?

Thanks if you answer :)/> !
Doyle3694 #2
Posted 23 December 2012 - 06:41 AM
the correct syntax for concatenation in lua is

(var1 .. " Yo! " .. var2)
Zambonie #3
Posted 23 December 2012 - 06:48 AM
Just for a second,can you explain that to me?
Zambonie #4
Posted 23 December 2012 - 07:22 AM
Acuttly,Im getting an another error.This time,Ill post the whole program.

term.clear()
term.setCursorPos(21,9)
print("Calculator")
sleep(2)
term.clear("21,9")
term.setCursorPos(1,1)
print("Please wnter you caculation.")
local input = read()
if input == (var1 .. " + " .. var2) then
print(var1 .. "+" .. var2)
end

and thats mainly it,and can you check it again if you made any mistakes or me?
remiX #5
Posted 23 December 2012 - 07:26 AM
Ok. So if you want print a normal string:
print("Hello there!")
You see there's no double dots (..)

Now when you want to concatenate a variable such as an input from an user:

print("Type something!")
input = read()
print("You have typed: " .. input)
Now you see I used a double dot (..) which is used to concatenate strings together, in this case it's used to concatenate a variable (the input)
Doyle3694 #6
Posted 23 December 2012 - 07:29 AM
what do you want it to detect? and where do you declare var1 and var2? var1 and var2 where only examples, they are not actually needed. Actually, go check out my tutorial about variables at the tutorials section, it covers concatenation. http://www.computercraft.info/forums2/index.php?/topic/7424-variables-what-are-they-and-how-do-we-use-them-also-covering-globals-and-locals/
Zambonie #7
Posted 23 December 2012 - 07:36 AM
So you mean I could type something like this?:

local input = read()
if input == (input1 "+" input2) then
print("input 1" + "input2")

Remember,im new to lua,and its kinda hard for me to learn it.Im trying and trying.
Doyle3694 #8
Posted 23 December 2012 - 07:40 AM
go through my tutorial, I think you'll find many new things there to learn, and it covers pretty much the area you are playing around in here.
Zambonie #9
Posted 23 December 2012 - 07:44 AM
Ya,im kinda discovering that,but in meantme,can you write the right code I need to use.Not just examples,but ones that I need for the calculater im making.I do here of something called of "io.read" or somthing,and that,I dont know but could be proboly a useful tip. :)/>
Doyle3694 #10
Posted 23 December 2012 - 07:51 AM

print("Welcome to your calculator")
write("Enter the first number: ")
num1 = tonumber(read())
write("Enter the second number: ")
num2 = tonumber(read())
write("What operation would you like to perform(add,subtract,multiply,divide)? ")
operation = read()
if operation == "add" then
   print(num1 .. " + " .. num2 .. " = "..num1+num2)
elseif operation == "subtract" then
   print(num1 .. " - " .. num2 .. " = "..num1-num2)
elseif operation == "multiply" then
   print(num1 .. " * " .. num2 .. " = "..num1*num2)
elseif operation == "divide" then
   print(num1 .. " / " .. num2 .. " = "..num1/num2)
else
   print("Unknown operation!")
end
Zambonie #11
Posted 23 December 2012 - 09:25 AM
Thanks,thats kinda what I was looking for.
Zambonie #12
Posted 23 December 2012 - 09:27 AM
You also made it easy enohg for me to understand! ;)/>