Easy question about gets

  • Thread starter Reiichi Reiichi
  • Start date
R

Reiichi Reiichi

(sorry my bad english) I am a beginner and in an exercise the one who
run the program must insert his birth date and the program must says if
today is his birthday or not.
In C to do this I can do (I jump a part of code):

printf("Insert your birth day (DD MM YYYY): ");
scanf("%d%d%d",&db,&mb,&yb);
printf("Insert the current day (DD MM YYYY): ");
scanf("%d%d%d",&dc,&mc,&yc);

(db = day birth; mb = month birth; dc = day current etc.)
And the rest of the code is not important for what I want to ask.
So, and in Ruby? I must use gets each time for any of DD, MM, and YYYY
separately?
There will be 6 gets, so many lines of code, is there any way to use
just 1 time gets for all the date like in C? Or some other way to write
less code? Thanks!
 
W

Wybo Dekker

require 'scanf'
def getdate(s)
print s
$stdout.flush
d,m,y = scanf("%d%d%d")
end

d,m,y = getdate("Your birthday? ")

p d,m,y
 
B

Brian Candler

Wybo said:
require 'scanf'
def getdate(s)
print s
$stdout.flush
d,m,y = scanf("%d%d%d")
end

d,m,y = getdate("Your birthday? ")

p d,m,y

Or at a higher level:

require 'date'
birthday = Date.parse(gets)
puts birthday.day, birthday.month, birthday.year

Warning: this accepts a lot of formats, including the unambiguous
yyyy/mm/dd, but seems to default to American middle-endian dates.

irb(main):013:0> birthday = Date.parse("01/02/2003")
=> #<Date: 4905283/2,0,2299161>
irb(main):014:0> puts birthday.day, birthday.month, birthday.year
2
1
2003
=> nil
 
N

Nick Brown

Reiichi said:
Thank you, it works! Ruby seams easy first, but is really complex *_*

This probably isn't the best example to demonstrate the overall
complexity of a language.

Try writing a webapp to do the same thing in Ruby and in C ;-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top