Can anyone please tell me what I should be using and why this method isn’t correct? The code doesn’t work, I know, I just want to know how I can interface with the array for this type of thing.
Thank you.
Code:
$i = 1
$f = 0
$c = 0
answer = ""
loop do
puts "Welcome\n"
puts "If you'd like to fly in first class, Press 1. For coach, Push 2."
answer = STDIN.gets
answer.chop!
break if answer =~ /1|2/
if answer == 1 then
$i += 1.each
$available.at(0..4)
end
if answer == 2 then
$i += 1.each
$available.at(5..19)
else
puts "Invalid number, enter 1 or 2."
Console_Screen.pause
end
end
puts "Here is your boarding pass. You are in seat " + $i.to_s
For first class, you can do something like this:
If that gives you a
next_availablethat isnilthen first class is full, otherwise you have the index of the next available first class seat.Similarly for coach:
Then once you have the index, have checked that it isn’t
nil, and stored it in$i, just$available[$i] = 1to reserve the seat.And an extra hint for free, this:
leaves a string in
answersoanswer == 1will always be false.References:
Enumerable#find