0

I am very new to programming, atleast with object oriented so please be gentle :)

I am trying to create a budgeting program and I am having trouble identifying strings. I have a variable called payperiod, simple enough it will be used to hold the string value of either weekly, monthly etc. What am I doing wrong here?

#!/usr/bin/env ruby
#
# Simple Budgeting Program
#


puts "Hi, I would like to develop a budgeting program for you..\n\n\n\n\n"
puts "What is your budget type? \n \n \n You can say 'Monthly' 'Weekly' or 'Fortnightly'  \n"
payperiod = gets

if payperiod == "Monthly"
  puts "You are paid monthly."
end

if payperiod == "Weekly"
  puts "You are paid Weekly"
end

if payperiod == "Fortnightly"
  puts "You are paid every two weeks!"
end
user2864740
  • 60,010
  • 15
  • 145
  • 220
dotKn0ck
  • 196
  • 1
  • 1
  • 12
  • 1
    Also http://ruby-doc.org/core-2.0/String.html#method-i-chomp – JKillian Jun 17 '14 at 22:04
  • http://stackoverflow.com/questions/11889594/ruby-gets-not-returning-correct-string?lq=1 , http://stackoverflow.com/questions/5304055/question-about-gets-in-ruby , http://stackoverflow.com/questions/17668971/ruby-gets-adds-newline-character?rq=1 – user2864740 Jun 17 '14 at 22:04
  • Then you may want to consider `if .. elsif` or [`case`](http://stackoverflow.com/questions/948135/how-can-i-write-a-switch-statement-in-ruby/948157#948157) – user2864740 Jun 17 '14 at 22:06
  • At least, the syntax is correct. It is not clear what it is that you are claiming to be wrong. – sawa Jun 18 '14 at 02:00

3 Answers3

1

You are forgetting about the return key you press to submit your answer:

2.1.0 :001 > mystring = gets
Monthly
 => "Monthly\n" 
2.1.0 :002 > mystring.chomp
 => "Monthly" 
2.1.0 :003 > correct_string = gets.chomp
Monthly         
=> "Monthly" 
JCorcuera
  • 6,794
  • 2
  • 35
  • 29
1

The gets method will gets even the "enter" keyboard key. It means that the special char \n will be added to the end of your string because that is the char that represents "enter" keyboard key.

Thus when you enter the words "Monthly", "Weekly" or "Fortnightly" what is really catch by the script is "Monthly\n", "Weekly\n" or "Fortnightly\n".

Thus your script must use chomp method (as highlited by JCorcuera) to delete that char.

Finishing if you want to use chomp method, your script will look like

payperiod = gets

if payperiod.chomp == "Monthly"
  puts "You are paid monthly."
elsif payperiod.chomp == "Weekly"
  puts "You are paid Weekly"
elsif payperiod.chomp == "Fortnightly"
  puts "You are paid every two weeks!"
else
  puts "Invalid option!"
end

If you don't want to use chomp you can append "\n"

payperiod = gets

if payperiod == "Monthly\n"
  puts "You are paid monthly."
elsif payperiod == "Weekly\n"
  puts "You are paid Weekly"
elsif payperiod == "Fortnightly\n"
  puts "You are paid every two weeks!"
else
  puts "Invalid option!"
end
Cristiano Mendonça
  • 1,220
  • 1
  • 10
  • 21
0

gets stands for get string. It returns everything that the user inputs, including things they don't realize they are putting in. gets.chomp removes all whitespace at the end so it would return only the string you want. It would also be a good idea to downcase it, incase the user puts in 'Monthly' 'monthly' or 'mOnthly'.

user_input: Monthly

mystring1 = gets
  => 'Monthly/n'
mystring2 = gets.chomp
  => 'Monthly'
mystring3 = gets.chomp.downcase
  => 'monthly'


mystring1 == 'monthly'
   => false
mystring1 == 'monthly'
   => false
mystring1 == 'monthly'
   => true