Banking on ruby getting user input


#1

Hello,

I was playing around the project banking on Ruby. I want to get user input for the password
Here's my code, it gave me the error of infinite loop. Could you help me understand why this is not working?

class Account
attr_reader :name
attr_reader :balance
def initialize(name,balance=100)
@name = name
@balance = balance
end

private
def pin
    @pin = 1234    
end

private
def check_pin
    puts "please input password"
    input_pin = gets.chomp.to_i
    input_pin == pin
end

private 
def pin_error
    return "Access denied: incorrect PIN."
end

public
def display_balance
    if check_pin
        puts "Balance: $#{@balance}"
    else
        pin_error
    end
end

public
def withdraw(pin_number,amount)
    if pin_number == pin
        @balance -= amount
        puts "#{name} Withdrew $#{amount}. New balance: $#{@balance}."
    else
        pin_error
    end
end

end

yaoie = Account.new("Yaoie")
yaoie.display_balance


#2

What do you mean by not working? What does it do differently from what you want? (working doesn't contain that information, it avoids it)


#3

I wanted to check if the input password from the user is the same as the pin method, so I created a method check_pin to get user input by using .get.chomp. But when the code was running, it put the "please input password" and not letting me input anything, and it went on and on, with the error appearing in the end to say "infinite loop"

I want to know why the get function doesn't seem to work inside the class

Thanks!


#4

i understand your post..but can you give more information regarding this banking on ruby.....


#5

Running your code goes like this:

please input password
1234
Balance: $100

And then it exits.

Keep in mind though that the instructions ask for some particular behaviour, perhaps you changed that behaviour, causing the test code to no longer succeed.


#6

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.