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

If Statement Wont Trigger

Started by schwem00, 11 January 2015 - 04:31 AM
schwem00 #1
Posted 11 January 2015 - 05:31 AM
So heres the issue. This program is meant to be put onto a disk. When started, it asks if you want to encrypt or decrypt. The encrypt section copies the computers ID and Label, then encrypts them. Next, it makes sure the "data" file does not already contain the encrypted ID and Label. If it dosen't, it writes the encrypted ID and Label to the file. This part works. However, if the user types "2", the program ends without an error messages. The "print" commands on lines 48, 55 and 62 do not show up, so the 2nd part is not even starting. I have no idea what is going on. Incase it helps, here is the encryption program.

http://www.computerc...sed-encryption/

For some reason, I can't get line numbers to show up. If you want to see which print commans I am talking about, click here.



local bool rep = false
local label = os.getComputerLabel()
local id = os.getComputerID()
local h = fs.open("/disk/data", "a")
local b = fs.open("/disk/data", "r")
local r = b.readAll()
local r2 = b.readLine()
local bool existing = false
local elabel = encrypt(label,"rofl")
local eid = encrypt(id,"rofl")
local encrypted = eid..elabel
local idlabel = id.." "..label

print("[1] Normal Usage")
print("[2] Decryption")
input2 = read()
if input2 == "1" then

print("Warning! Do not use numbers in labels!")
print("Continue?  Y/N")
input = read()

if input == "Y" or input == "y" then

if string.find (r, encrypt(label,"rofl")) or string.find (r, encrypt(id,"rofl")) then
existing = true
end


if label ~= nil and existing ~= true then
local text = encrypted
h.writeLine(text)
h.close()

elseif label == nil then

print("Please add a label.")

elseif existing == true then

print("Computer already on file.")
else
print ("Unknown Error")

end
end
if input2 ~= "1" then
print("Unknown Error")
-- This error code never triggers, so the if statement always fails. Why?
printdec()

end

if decrypt(r,"rofl") == encrypted then
print("Error Decrypting")
-- This dosen't trigger either.
end
end

function printdec()
-- This message dosen't print.
print("Decrypting..")
while rep == true do
local t = r2

if t == nil then
rep = false
else
print(decrypt(t,"rofl"))
end
end
end

-- Had to do all this because print(decrypt(r,"rofl")) wouldn't decrypt properly.

Edited on 11 January 2015 - 03:14 PM
Dragon53535 #2
Posted 11 January 2015 - 06:04 AM
Indenting your code shows us the problem


local bool rep = false
local label = os.getComputerLabel()
local id = os.getComputerID()
local h = fs.open("/disk/data", "a")
local b = fs.open("/disk/data", "r")
local r = b.readAll()
local r2 = b.readLine()
local bool existing = false
local elabel = encrypt(label,"rofl")
local eid = encrypt(id,"rofl")
local encrypted = eid..elabel
local idlabel = id.." "..label
print("[1] Normal Usage")
print("[2] Decryption")
input2 = read()
if input2 == "1" then
  print("Warning! Do not use numbers in labels!")
  print("Continue?  Y/N")
  input = read()
  if input == "Y" or input == "y" then
    if string.find (r, encrypt(label,"rofl")) or string.find (r, encrypt(id,"rofl")) then
	  existing = true
    end

	  if label ~= nil and existing ~= true then
	    local text = encrypted
	    h.writeLine(text)
	    h.close()
	  elseif label == nil then
	    print("Please add a label.")
	  elseif existing == true then
	    print("Computer already on file.")
	  else
	    print ("Unknown Error")
	  end
    end
  if input2 ~= "1" then
    print("Unknown Error")
  -- This error code never triggers, so the if statement always fails. Why?
  printdec()
  end
  if decrypt(r,"rofl") == encrypted then
    print("Error Decrypting")
  -- This dosen't trigger either.
  end
end
function printdec()
  -- This message dosen't print.
  print("Decrypting..")
  while rep == true do
    local t = r2
    if t == nil then
	  rep = false
    else
	  print(decrypt(t,"rofl"))
    end
  end
end
Your two if statements you're worried about, are all inside the first

if input == "1" then
You want them to actually be elseif's and then you also need to remove their respective ends.

local bool rep = false
local label = os.getComputerLabel()
local id = os.getComputerID()
local h = fs.open("/disk/data", "a")
local b = fs.open("/disk/data", "r")
local r = b.readAll()
local r2 = b.readLine()
local bool existing = false
local elabel = encrypt(label,"rofl")
local eid = encrypt(id,"rofl")
local encrypted = eid..elabel
local idlabel = id.." "..label
print("[1] Normal Usage")
print("[2] Decryption")
input2 = read()
if input2 == "1" then
  print("Warning! Do not use numbers in labels!")
  print("Continue?  Y/N")
  input = read()
  if input == "Y" or input == "y" then
    if string.find (r, encrypt(label,"rofl")) or string.find (r, encrypt(id,"rofl")) then
	  existing = true
    end

	  if label ~= nil and existing ~= true then
	    local text = encrypted
	    h.writeLine(text)
	    h.close()
	  elseif label == nil then
	    print("Please add a label.")
	  elseif existing == true then
	    print("Computer already on file.")
	  else
	    print ("Unknown Error")
	  end
    end
elseif input2 ~= "1" then
  print("Unknown Error")
  -- This error code never triggers, so the if statement always fails. Why?
  printdec()

elseif decrypt(r,"rofl") == encrypted then
  print("Error Decrypting")
-- This dosen't trigger either.
end
function printdec()
  -- This message dosen't print.
  print("Decrypting..")
  while rep == true do
    local t = r2
    if t == nil then
	  rep = false
    else
	  print(decrypt(t,"rofl"))
    end
  end
end
schwem00 #3
Posted 11 January 2015 - 03:37 PM
Thanks! I think it used to be an elseif, but I must have accidentally changed it when I got an <eof> error.