Jump to content




User input to strings?


  • You cannot reply to this topic
11 replies to this topic

#1 Zambonie

  • Members
  • 477 posts
  • LocationSpring Hill, Fl

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

#2 Doyle3694

  • Members
  • 815 posts

Posted 23 December 2012 - 06:41 AM

the correct syntax for concatenation in lua is
(var1 .. " Yo! " .. var2)


#3 Zambonie

  • Members
  • 477 posts
  • LocationSpring Hill, Fl

Posted 23 December 2012 - 06:48 AM

Just for a second,can you explain that to me?

#4 Zambonie

  • Members
  • 477 posts
  • LocationSpring Hill, Fl

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?

#5 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

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)

#6 Doyle3694

  • Members
  • 815 posts

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.computerc...als-and-locals/

#7 Zambonie

  • Members
  • 477 posts
  • LocationSpring Hill, Fl

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.

#8 Doyle3694

  • Members
  • 815 posts

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.

#9 Zambonie

  • Members
  • 477 posts
  • LocationSpring Hill, Fl

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

#10 Doyle3694

  • Members
  • 815 posts

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


#11 Zambonie

  • Members
  • 477 posts
  • LocationSpring Hill, Fl

Posted 23 December 2012 - 09:25 AM

Thanks,thats kinda what I was looking for.

#12 Zambonie

  • Members
  • 477 posts
  • LocationSpring Hill, Fl

Posted 23 December 2012 - 09:27 AM

You also made it easy enohg for me to understand! ;)





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users