Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trouble with accepting user input

Status
Not open for further replies.

invalid6363

Programmer
Mar 21, 2008
24
US
hello, i'm new to ruby and i'm trying a to write a simple greeting program that accepts input from the user (user's name) but its not completely working the way i would like it to. It doesn't print out the goodbye messages and doesn't work when nothing is typed in (Hello World should print out).

Ruby:
#!/usr/bin/env ruby

class SuperGreeter
  
  print "Enter your name: "
  @name = gets
  
  # Say hello
    if @name == ""
      puts "Hello World!"
    else
      puts "Hello #{@name}"
      puts "Nice to meet you."
    end
  
  print "Are you staying?[y/n]: "
  @answer = gets
  
  # Say Goodbye
    if @answer == ""
      puts "Goodbye World!"
    elsif @answer == "y"
      puts "Thanks for sticking around."
    elsif @answer == "n"
      puts "Goodbye #{@name}, and come back soon!"
    end

end

if someone can help me i would really appreciate it.
Thank you.
 

Oh, ok. Thanks for your help. I made the change in the code and it works perfectly now. :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top