0

the code below will put names into groups (e.g. first person goes into first group, second person goes into second group etc).

I'd like to enter the final piece of code and request user to input the number of the group. This should then print the people in that group, each separated by a comma and a space. Group numbers are "1-indexed". This means that, if the user enters 1, the first group should be printed, not the second group. Keep on asking the user for (final) group numbers until the user enters stop.

puts "How many groups would you like?"
group_num = gets.chomp.to_i

array = Array.new(group_num) { [] }

puts "Enter one name at a time"
count = 0
 while input_name = gets.chomp
  if input_name == "stop"
    break
  else puts "Give me a name"
   array[count] << input_name
   count += 1
   count = 0 if count == group_num
  end
 end

array.inspect

So if array = [["John", "Steve"], ["Judy", Pete"]] and the group number requested is 2, the output should print: "Judy, Pete" (on the same line).

5
  • does each group always contain just 2 names? Commented May 8, 2019 at 13:28
  • No, there can be as many groups and names as specified by the user. The final piece of code just needs to request which group number the user would like to see. Everything else has already been achieved in the original code. Commented May 8, 2019 at 13:33
  • so how do you know when a group is done entering names? Commented May 8, 2019 at 13:37
  • I have the solution now, but thanks for looking. Commented May 8, 2019 at 13:39
  • See my answer anyway, it might help. Also it's a good idea on Stackoverflow to allow time for other possible answers before marking the very first answer as "accepted" as there are many ways to solve the same problem. Commented May 8, 2019 at 13:41

2 Answers 2

1

Given the array and the index wanted is very simple

array = [["John", "Steve"], ["Judy", "Pete"]]
puts "tell me which group you want with a number"
number = gets.to_i
if (1..array.length).include?(number)
  puts "people: #{array[number - 1].join(", ")}"
else
  puts "Number element not present"
end
Sign up to request clarification or add additional context in comments.

2 Comments

What I need is for the input to be requested from the user. So for example: puts "Enter your group number" and then this will return the new group of names. I know it should be pretty simple with "gets"...
I updated my answer. It seems to me you already know how to ask for an input and gets it. Anyway, I think you just need to paste my code at the end of your and remove my first line
0

I think this is what you want you program to do? Try to run it:

array = []
puts "How many groups would you like?"
group_num = gets.chomp.to_i

group_num.times do
  puts "\nEnter one name at a time"
  a = []
  loop do
    puts "Give me a name or 'stop' to stop adding names"
    input_name = gets.chomp
    break if input_name == "stop"
    a << input_name
  end
  array << a
end

array.each_with_index{|a, i| puts "#{i+1}. #{a}" }
puts "select number which group you want"
group = gets.chomp.to_i - 1
puts "you selected group: #{array[group].to_s}"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.