Hi guys, I have a code in ruby that reads from a file and then prints out the information as an array in the terminal. That part seems to work fine. However, the code should also prompt the user for input and then match the input with the information in the array. Right now, I am getting “Entry not found” regardless of the input.
The text file:
Neil Diamond Greatest Hits 1 3 Crackling Rose sounds/01-Cracklin-rose.wav Soolaimon sounds/06-Soolaimon.wav Sweet Caroline sounds/20-Sweet_Caroline.wav
require './input_functions' module Genre POP, CLASSIC, JAZZ, ROCK = *1..4 end class Album # you will need to add tracks to the following and the initialize() attr_accessor :title, :artist, :genre, :tracks # complete the missing code: def initialize (title, artist, genre, tracks) # insert lines here @title = title @artist = artist @genre = genre @tracks = tracks end end class Track attr_accessor :name, :location def initialize (name, location) @name = name @location = location end end # Reads in and returns a single track from the given file def read_track(music_file) track_name=music_file.gets.chomp track_location = music_file.gets.chomp track = Track.new(track_name, track_location) track.name= track_name track.location= track_location return track end # Returns an array of tracks read from the given file def read_tracks(music_file) count = music_file.gets().to_i() tracks = Array.new() i=0 while (i<count) track = read_track(music_file) tracks << track i+=1 end return tracks # Put a while loop here which increments an index to read the tracks end # Takes an array of tracks and prints them to the terminal def print_tracks(tracks) index = 0 while (index<tracks.length) print_track (tracks[index]) index+=1 end . end # Reads in and returns a single album from the given file, with all its tracks def read_album(music_file) album_title = music_file.gets album_artist = music_file.gets album_genre = music_file.gets.chomp.to_i tracks=read_tracks(music_file) album_tracks=tracks album = Album.new(album_title, album_artist, album_genre, album_tracks) album.title = album_title album.artist = album_artist album.genre = album_genre album.tracks = album_tracks return album end # Takes a single album and prints it to the terminal along with all its tracks def print_album(album) genre_names = ['Null', 'Classic', 'Pop', 'Jazz', 'Rock'] # print out all the albums fields/attributes # Complete the missing code. puts album.title puts album.artist puts('Genre is ' + album.genre.to_s()) puts(genre_names[album.genre]) tracks=album.tracks print_tracks(tracks) # print out the tracks end # Takes a single track and prints it to the terminal def print_track(track) puts(track.name) puts(track.location) end # search for track by name. This seems to be problem # Returns the index of the track or -1 if not found def search_for_track_name(tracks, search_string) index = 0 found_index=-1 while (index<tracks.length) if (tracks[index]==search_string) found_index=index end index+=1 return found_index end end # Reads in an Album from a file and then prints all the album # to the terminal def main() music_file = File.new("album.txt", "r") album = read_album(music_file) print_album(album) music_file.close() puts "Enter the track name you wish to find: " search_name = gets.chomp index = search_for_track_name(album.tracks, search_name) if index > -1 puts "Found " + album_tracks[index].name + " at " + index.to_s() else puts "Entry not Found" end end main()