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

For input string "1shell"

Started by El__Nacho, 23 July 2014 - 03:19 PM
El__Nacho #1
Posted 23 July 2014 - 05:19 PM
Hey guys i have a problem which i don't understand. Everytime i run the program and type "Anlagen verwalten" it leaves the program and prints "For input string "1shell" ". Do you know where the mistake is?

c = 1
while c ==1 do
  c = 0
  shell.run("clear")
  print("Was willst du tun?")
  print(" ")
  print(" - Anlagen verwalten")
  print(" - Programm verlassen")
  print(" ")
  write("Schreibe deine Auswahl: ")
  a = io.read()
  if a == "Anlagen verwalten" then
    shell.run("1shell")
  elseif a == "Programm verlassen" then
    shell.run("clear")
  else
    print("Falsche Eingabe")
    sleep(2.5)
  c = 1
  end
end
Edited on 23 July 2014 - 05:37 PM
flaghacker #2
Posted 23 July 2014 - 07:10 PM
For future posts (and maybe edit this one?) use code tags:
[ code] code here 
, without the spaces in the [ ].

I don't see any mistakes, is it printed in red with a line number? Can you post the code of the program "1shell"?
Edited on 23 July 2014 - 05:11 PM
El__Nacho #3
Posted 23 July 2014 - 07:45 PM

c = 0
while c == 0 do
  c = 1
  shell.run("clear")
  print("...")
  print("...")
  write("...")
  a = io.read()
  if a == "Raffinerie" then
	shell.run("Raffinerie")
  elseif a == "Steinbruch" then
	shell.run("Steinbruch")
  else
	print("Falsche Eingabe")
	c = 0
  end
end
Edited on 23 July 2014 - 06:21 PM
Lyqyd #4
Posted 23 July 2014 - 07:50 PM
You've run two lines together, where `c = 1` and the line below it are on one line. Make them two lines again.
El__Nacho #5
Posted 23 July 2014 - 08:27 PM
Thank you :D/> Now it works.
SquidDev #6
Posted 23 July 2014 - 08:55 PM
On a side note: Don't use shell.run("clear"). It is better to use:

term.clear()
term.setCursorPos(1, 1)
flaghacker #7
Posted 23 July 2014 - 09:10 PM
On a side note: Don't use shell.run("clear"). It is better to use:

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

Why? Does that runs faster?
Edited on 23 July 2014 - 07:11 PM
Lyqyd #8
Posted 23 July 2014 - 10:06 PM
For one, yes. For another, that's all the clear program does anyway, so why involve the shell at all? If you're clearing the screen often enough that you can only stand to use one line of code when you do it, you could even declare your own clear function that does those two lines. There's no good reason to run another program instead of just clearing the screen in your own program.
Graypup #9
Posted 23 July 2014 - 10:50 PM

c = true
while c do
  c = true
  shell.run("clear")
  print("...")
  print("...")
  write("...")
  a = io.read()
  if a == "Raffinerie" then
	shell.run("Raffinerie")
  elseif a == "Steinbruch" then
	shell.run("Steinbruch")
  else
	print("Falsche Eingabe")
	c = true
  end
end
Side note: lua has booleans, true and false. So you could replace c == 0 in the while declaration with simply c, and if c is true, it will run, otherwise it won't.
SquidDev #10
Posted 24 July 2014 - 07:38 AM

-snip-
Side note: lua has booleans, true and false. So you could replace c == 0 in the while declaration with simply c, and if c is true, it will run, otherwise it won't.

When c=0, c evaluates to false, so it should probably be while not c do instead.
Bomb Bloke #11
Posted 24 July 2014 - 11:25 AM
You might be confusing 0 with nil, perhaps.
MKlegoman357 #12
Posted 24 July 2014 - 01:52 PM
When c=0, c evaluates to false, so it should probably be while not c do instead.

In Lua only false and nil evaluate to false, anything else evaluates to true. So 0 would still evaluate to true.
TheOddByte #13
Posted 24 July 2014 - 10:46 PM
I want to add something too, you should really use locals

local foo = "bar"
So why is locals good? Well for one this makes it accessible only from the program, so for example if you would create a program with a password assigned to a variable and it wasn't localized.. well then it would be very easy to access. I'm not good at explaining things so I suggest you check out the link above

Also, don't make your variable local in a statement, loop or function unless you need to, the foo in the statement/loop/function is local to that only if you declare it local there

local foo = "bar"
if 1 == 1 then
    local foo = "Hello World!"
end
print( foo )
-- Outputs "bar"

local foo = "Hello World!"
if 1 == 1 then
    foo = "bar"
end
print( foo )
--Outputs "bar"

And here's a suggestion, you could remove the variable 'c' and use break

while true do -- infinite loop
    if 1 + 1 ~= 1 then
        doSomething() -- do the stuff before exiting the loop
        break -- exit the loop
    end
end