Home > Enterprise >  Why is my ruby code getting error messages?
Why is my ruby code getting error messages?

Time:10-19

require './input_functions'

# Complete the code below
# Use input_functions to read the data from the user
class Flight
def initialize(id, flightNumber, originAirport, destinationAirport)
@id = id
@flightNumber = flightNumber
@originAirport = originAirport
@destinationAirport = destinationAirport
end
def id
@id
end
def flightNumber
@flightNumber
end
def originAirport
@originAirport
end
def destinationAirport
@destinationAirport
end
end

def read_flight()
    id = read_integer("Enter plane id:")
    flightName = read_string("Enter flight name:")
    originAirport = read_string("Enter origin airport:")
    destinationAirport = read_string("Enter destination airport:")
    return Flight.new(id,flightName,originAirport,destinationAirport)
end

def read_flights()
    numOfFlights = read_integer("")
    flights = Array.new(numOfFlights)
    for i in 0..numOfFlights-1
    flights[i] = read_flight()
end
return flights

def print_flight(flight)
    puts "Plane id #{flight.id}"
    puts "Flight #{flight.flightNumber}"
    puts "Origin #{flight.originAirport}"
    puts "Destination #{flight.destinationAirport}"
end

def print_flights(flights)
    flights.each do |flight|
    print_flight(flight)
end

def main()
    flights = read_flights()
    print_flights(flights)
end

main()
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

# Display the prompt and return the read string
def read_string prompt
    puts prompt
    value = gets.chomp
end

# Display the prompt and return the read float
def read_float prompt
    value = read_string(prompt)
    value.to_f
end

# Display the prompt and return the read integer
def read_integer prompt
    value = read_string(prompt)
    value.to_i
end

# Read an integer between min and max, prompting with the string provided

def read_integer_in_range(prompt, min, max)
    value = read_integer(prompt)
    while (value < min or value > max)
        puts "Please enter a value between "   min.to_s   " and "   max.to_s   ": "
        value = read_integer(prompt);
    end
    value
end

# Display the prompt and return the read Boolean

def read_boolean prompt
    value = read_string(prompt)
    case value
    when 'y', 'yes', 'Yes', 'YES'
        true
    else
        false
    end
end

# Test the functions above
=begin
def main
    puts "String entered is: "   read_string("Enter a String: ")
    puts "Boolean is: "   read_boolean("Enter yes or no:").to_s
    puts "Float is: "   read_float("Enter a floating point number: ").to_s
    puts "Integer is: "   read_integer_in_range("Enter an integer between 3 and 6: ", 3, 6).to_s
end

main
=end
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

Error message: flight.rb:59: syntax error, unexpected end-of-input, expecting `end'

I've completely finished my code but I can't seem to find what is causing the errors? I'm assuming the fault lies entirely in flight.rb, which is the first snippet. Please explain what I did wrong instead of only sending the answer, thank you.

CodePudding user response:

You missed the closing end for read_flights:

def read_flights()
    numOfFlights = read_integer("")
    flights = Array.new(numOfFlights)
    for i in 0..numOfFlights-1
      flights[i] = read_flight()
    end
    return flights
end # <- missed end
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related