Here's what I have so far:
#Table of Contents using an array
title = "Table of Consents"
#Needs chapters inputted
chapters = Array.new
puts "Please input chapter names."
while gets.chomp != ""
chapter = gets.chomp
chapters.push chapter
break if gets.chomp.empty?
end
#Needs corresponding page numbers inputted
pagenumbers = Array.new
puts "Please input corresponding page numbers."
while gets.chomp != ""
pagenum = gets.chomp
pagenumbers.push pagenum
break if gets.chomp.empty?
end
puts chapters
puts page numbers
I'm trying to get gets.chomp
to continue adding to each array. When I push chapter
or pagenum
, I only get the last string/integer input. How do I get each input to populate the chapter
or pagenum
array?
The problem here is that every single time you write gets
in your program, you are calling the gets
method, which fetches a line of input from the user. You need to store that line somewhere so it can be used later. Try something like this:
chapters = []
while true
input = gets.chomp
break if input.empty?
chapters << input
end
puts "Chapters: " + chapters.join(", ")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.