23 posts
Posted 14 February 2015 - 06:03 AM
I'm trying to write a program that calculates time and hourly rate then saves information to log files.
http://pastebin.com/0dPB7jKHThe program runs perfectly until it gets to around line 95. I don't have a very good understanding of the fs API so I expected to make mistakes, but I don't know how to fix it or where the problem is. It's also very possible that I made more mistakes in the code toward the bottom of the program. Help would be greatly appreciated.
8543 posts
Posted 14 February 2015 - 06:20 AM
The fs.open call returns a file handle, which you have to put into a variable. It looks like your code expects that variable to be h, so put the return value from your fs.open calls into that variable. For example, swap out line 104 for something like this:
local h = fs.open("rate", "w")
23 posts
Posted 14 February 2015 - 06:37 AM
I looked at what you said, looked back at my program, and everything suddenly made sense. Thank you sir. I have one more question though. After I fixed it, I checked the log and the total time was written with decimal points, like this: "11.0:16.0:21.0". What should I do to make those whole numbers without decimal points?
3057 posts
Location
United States of America
Posted 14 February 2015 - 07:24 AM
I suggest you look at
this function. It'll let you save a table which can then be loaded using the unserialize counterpart function.
If you insist on using the current system, string.gmatch( str, pattern ) might be of interest.
pattern tutorial.
7083 posts
Location
Tasmania (AU)
Posted 14 February 2015 - 11:03 AM
You should be able to change line 131 like so:
totaltime = tostring(hr)..":"..tostring(min)..":"..tostring(sec)
That would allow you to turn this sort of thing:
write("Total: ")
for q=1,5 do
write(totaltime[q])
end
print("")
… into this:
print("Total: "..totaltime)
23 posts
Posted 14 February 2015 - 05:08 PM
I actually just ended up changing line 174-177 to one line like this:
l.writeLine("Total: "..hr..":"..min..":"..sec)
That seems to have fixed it. It was only the log that was showing decimals, but not what appeared on the console when I run the program. Everything seems to be working properly now. Thank you all for the help!