0

So this is my code. It's pretty self-explanatory.

print "How old are you? "
age = gets.chomp()
print "How tall are you?"
height = gets.chomp()
print "How much do you weigh?"
weight = gets.chomp()
puts "So, you're #{age} old, #{height} tall and #{weight} heavy."

My code output is as follows.

$  C:/Ruby200/bin/ruby.exe ex11.rb
11
11
11
How old are you? How tall are you?How much do you weigh?So, you're 11 old, 11 tall and 11 heavy.

It's probably a really simple error, but I would be grateful if you can point it out.

1 Answer 1

1

I take it that your question is: "All my prompts are printed all at once after all inputs. What's up with that?". I have an answer for you then :)

print doesn't add a newline to the string. And STDOUT doesn't flush until it's got a full line. Simple fix: replace print with puts (which does add newline char)

puts "How old are you? "
age = gets.chomp()
puts "How tall are you?"
height = gets.chomp()
puts "How much do you weigh?"
weight = gets.chomp()
puts "So, you're #{age} old, #{height} tall and #{weight} heavy."
Sign up to request clarification or add additional context in comments.

8 Comments

Do you know any setting for Notepad++,as TextMate has ? Like if I write 2 + 3 # => and run the code, would get 2 + 3 # => 5
@Priti: nope, i have no idea
Sorry, it doesn't quite seem to work. I understand why it has to be puts but I keep getting the same error in Cygwin. I have run the new code on an online IDE and it works there.
What error is that? Works for me. Probably it's something with your local env (Cygwin)
All my prompts are still printed after my inputs in Cygwin.
|

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.