I'm trying to write a program that asks users to enter words, and then I want the words entered to be stored into an array which is later to be printed on the screen after they press the return key.
Here is the code logic I am using now, but I'm just struggling to figure this out.
while loop do
puts "Enter your words: "
user_input = gets.chomp.capitalize
if user_input != ""
user_words = Array.new
elsif user_input == ""
There are many problems with your code, here they are:
loopin one statement is useless
puts 'Enter your words:'should be declared outside of the loop
user_wordsshould be declared outside of the loop to accumulate the values
puts user_words.to_sshould be outside of the loop to print all values in the very end
pushshould be called on an array not on a string
elsif user_input == ""is excessive, could be replaced with
Array.newis worse than
Here is my solution:
puts 'Enter your words:' words =  loop do input = gets.chomp.capitalize break if input == '' words << input end puts words.to_s
I hope it helps.