Need Help with another Simple Program

As an exercise out of Chris P.'s ‘Learning to Program,’ I’ve attempted
to build a program that calculates the number of leap years between two
given years. Here is my code:

puts ‘Use this calculator to determine the number of leap years in a
specific period.’
total = 0 # total number of leap years
puts ‘Enter the first year below:’
year_one = gets.chomp.to_i
puts ‘Enter the second year below:’
year_two = gets.chomp.to_i

while year_one.to_i < year_two.to_i
if year_one%4 == 0
if year_one%100 != 0 || year_one%400 == 0
total = total + 1
year_one = year_one+ 1
else
year_one = year_one+ 1
end
end
end
puts 'Between ’ + year_one.to_s + ’ and ’ + year_two.to_s + ‘, there
were ’
puts total.to_s + ’ leap years.’

This is a different approach than Pine takes (most of my attempts are
formulated differently, but based around the right concept). He does
this:
puts ‘Pick a starting year (like 1973 or something):’
starting = gets.chomp.to_i
puts ‘Now pick an ending year:’
ending = gets.chomp.to_i
puts ‘Check it out… these years are leap years:’
year = starting
while year <= ending
if year%4 == 0
if year%100 != 0 || year%400 == 0
puts year
end
end
year = year + 1
end

What’s going wrong?

On Fri, Jul 17, 2009 at 9:52 PM, Max N.[email protected]
wrote:

 year_one = year_one+ 1

end
end
end

What’s going wrong?

Consider this snippet:

i = 0
while i < 1
if false
i += 1 # oops, we never get here
end
end

Consider this snippet:

i = 0
while i < 1
if false
i += 1 # oops, we never get here
end
end

I’m not sure I understand this. What does that snippet exemplify?

Max N. wrote:

Consider this snippet:

i = 0
while i < 1
if false
i += 1 # oops, we never get here
end
end

I’m not sure I understand this. What does that snippet exemplify?

Look at this:

while year_one.to_i < year_two.to_i
if false
if year_one%100 != 0 || year_one%400 == 0
total = total + 1
year_one = year_one+ 1
else
year_one = year_one+ 1
end

Also, what value is stored in the variable year_one right before the
while loop starts?

Max N. wrote:

I think I’ve determined my problem; it relates to the variable
‘year_one.’ year_one is supposed to be the first year entered when the
program starts.

And what was year_one in your first post?

Look at this:

while year_one.to_i < year_two.to_i
if false
if year_one%100 != 0 || year_one%400 == 0
total = total + 1
year_one = year_one+ 1
else
year_one = year_one+ 1
end

Also, what value is stored in the variable year_one right before the
while loop starts?

I think I’ve determined my problem; it relates to the variable
‘year_one.’ year_one is supposed to be the first year entered when the
program starts.

Check out my revision, which appears to work:

puts ‘Use this calculator to determine the leap years in a specific
period.’
puts ‘Enter the starting point below:’
start = gets.chomp.to_i
puts ‘Enter the ending point below:’
ending = gets.chomp.to_i

year = start
total = 0

while year <= ending
if year%4 == 0
if year%100 != 0 || year%400 == 0
puts year
total = total + 1
end
end

year = year + 1

end

if total == 1
puts 'There is 1 leap year between ’
puts start.to_s + ’ and ’ + ending.to_s + ‘.’
else
puts 'There are ’ + total.to_s + ’ leap years between ’
puts start.to_s + ’ and ’ + ending.to_s + ‘.’
end

On Sat, Jul 18, 2009 at 12:33 AM, Max N.[email protected]
wrote:

Consider this snippet:

i = 0
while i < 1
if false
i += 1 # oops, we never get here
end
end

I’m not sure I understand this. What does that snippet exemplify?

In this snippet the loop exit condition will never be occur because i
will not be incremented. Your original version had a similar error:

while
if # when this is false
if
year_one = year_one+ 1 # we never get to here
else
year_one = year_one+ 1 # or here, so we keep looping
end#inner if
end#outer if
end#while

I wasn’t sure how much you would want to be given away, so I tried to
keep my example abstract. :slight_smile:

I wasn’t sure how much you would want to be given away, so I tried to
keep my example abstract. :slight_smile:

I appreciate your courtesy. I figured out my the problem a little while
ago, but your explanation is still useful.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs