Go Back   CodingForums.com > :: Server side development > Ruby & Ruby On Rails

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-12-2009, 02:30 PM   PM User | #1
DayOne
New to the CF scene

 
Join Date: Feb 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
DayOne is an unknown quantity at this point
Please suggest newbie forum

Hi

I just started learning programming with a web tutorial here:

http://pine.fm/LearnToProgram/?Chapter=00

It's great but I one of the assignments (the Leap Years program at the end of chapter six) was too difficult and my code produces an endless loop!

It's a very simple program and I am very new to this so I didn't want to post it here where everyone seems to be "on rails" so to speak. Does anyone know a forum which is dedicated to helping beginners where I could maybe post my code and ask people to tell me where I 've gone wrong?

Thanks!

Last edited by DayOne; 02-12-2009 at 02:53 PM..
DayOne is offline   Reply With Quote
Old 02-12-2009, 04:20 PM   PM User | #2
CodeSpawn
New Coder

 
Join Date: Feb 2009
Posts: 30
Thanks: 1
Thanked 7 Times in 7 Posts
CodeSpawn has a little shameless behaviour in the past
I think You Can Use This Place For That.
But I just created my own forum. http://www.allcodeforum.co.nr I am trying to get more members so yea. It is dedicated to helping everyone in every programming language. I am good at most languages including ruby so I could help you. But I do want more members though.

Last edited by CodeSpawn; 02-12-2009 at 04:29 PM..
CodeSpawn is offline   Reply With Quote
Old 02-13-2009, 01:42 AM   PM User | #3
DayOne
New to the CF scene

 
Join Date: Feb 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
DayOne is an unknown quantity at this point
Thanks, CodeSpawn. This place seemed too advanced for my question but I'll post my code here and see if anyone can help.
BTW, at the moment I wouldn't know where to post Ruby questions on your forum.

Here's my broken code anyway:

Code:
puts 'This program calculates leap years between 2 dates'

startyear = 0
endyear = 0

puts 'Enter start year:'
startyear = gets.chomp

if (startyear.to_i <= 0)
  puts 'Your start year must be 1 AD or later!'
  puts 'Re-enter start year:'
  startyear = gets.chomp
  
end  
puts 'Enter end year'
endyear = gets.chomp
if (endyear.to_i <= startyear.to_i)  
  puts 'Your end year must be later!'
  puts 'Re-enter end year'
  endyear = gets.chomp
  
end

puts 'So that\'s leap years between ' + startyear + ' and ' + endyear + '?'
puts 'OK, cool.'

countstart = startyear.to_i
countend = endyear.to_i

# check if its a leap year. Leap years are years divisible by four 
# unless divisible by 100 
# unless they are divisible by 400

  while countstart <= countend
# check if its a leap year
    if (countstart % 4 != 0) or ((countstart % 100 == 0) and (countstart % 400 != 0))
  
    puts countstart.to_s + ' is not a leap year'
    
    else 
    puts countstart
    end
    countstart = countstart += 1
    
  end
  puts 'that is all'
end

Last edited by DayOne; 02-13-2009 at 01:45 AM..
DayOne is offline   Reply With Quote
Old 02-13-2009, 05:43 PM   PM User | #4
CodeSpawn
New Coder

 
Join Date: Feb 2009
Posts: 30
Thanks: 1
Thanked 7 Times in 7 Posts
CodeSpawn has a little shameless behaviour in the past
That is simple. First Of I forgot I just added Ruby to COMPUTER PROGRAMMING LANGUAGES. Lol I comleatly forgot about Ruby. It is still a new forum. So all the programming languages in the world can't be there from the beggining. But I am working on it.

As for you problem. I see only 2 things wrong with it. The code that calculates the leap years isn't bad. But I must admit it is annoying to see all the years and it saying is not a leap year.

So I would just take out this entire line.
Quote:
puts countstart.to_s + ' is not a leap year'
Other then that. It seems to work. The reason why it might not work for you is because at first the compiler whined to me. Something about unexpected kEND expected $end. So I replaced the last line of your code from
Quote:
end
to
Quote:
$end
And it works perfectly. Nice code BTW.
CodeSpawn is offline   Reply With Quote
Old 02-14-2009, 11:10 AM   PM User | #5
DayOne
New to the CF scene

 
Join Date: Feb 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
DayOne is an unknown quantity at this point
Thumbs up

Thanks very much!!!! It works! I don't think I could have figured that out by myself.

The "(Year) is not a leap year" is totally annoying. You're right. It wasn't in my original code.When it didn't work, I added that line to see what was happening to the loop when the program was running. I'll take it out again so I don't annoy myself!

Good luck with your forum. I am gonna have some more Ruby questions in the future, for sure.

Thanks again.
DayOne is offline   Reply With Quote
Old 02-15-2009, 06:48 PM   PM User | #6
CodeSpawn
New Coder

 
Join Date: Feb 2009
Posts: 30
Thanks: 1
Thanked 7 Times in 7 Posts
CodeSpawn has a little shameless behaviour in the past
I'm glad that it is working for you. Good luck learning Ruby.
CodeSpawn is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:53 AM.


Advertisement
Log in to turn off these ads.