select case construct vb - days in a month

code for how many days in a month with case select

Page 1 of 1

12 Replies - 7206 Views - Last Post: 22 March 2009 - 09:54 AM Rate Topic: -----

#1 kaybear912  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 07-February 09

select case construct vb - days in a month

Post icon  Posted 13 February 2009 - 06:18 PM

I need to use select case to input a month by using its number IE: january = 1, february = 2 etc.
when the month is inputted, how many days in that month is the output. I have to take into consideration leap year. the years I am using to determine leap year is 2004,2008,2012,2016
Here is the code I have so far:
Module Module1

	Sub Main()
		Dim monthnumber As Integer
		Dim year As Integer
		Dim numberofdays As Integer
		Console.WriteLine("Please enter Month number:January=1,February=2 etc.")
		monthnumber = (Console.ReadLine())
		Console.WriteLine("Please enter year:")

		year = Convert.ToBoolean(Console.ReadLine())

		Select Case monthnumber
			Case 1, 3, 5, 7, 8, 10, 12
				numberofdays = "There are 31 days in this month."
			Case 4, 6, 9, 11
				numberofdays = "There are 30 days in this month."
			Case 2
				Select Case year
					Case 2004, 2008, 2012, 2016
						numberofdays = 29
					Case Else
						numberofdays = 28
				End Select
		End Select


	End Sub

End Module




I know it is wrong, but not sure what I am doing wrong.

Is This A Good Question/Topic? 0
  • +

Replies To: select case construct vb - days in a month

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,500
  • Joined: 29-May 08

Re: select case construct vb - days in a month

Posted 13 February 2009 - 06:47 PM

Its very good.
You just need to output it to the console.
Console.Writeline("Number of Days In Month is {0}",numberofdays)



It lacks sanity checks for months < 1 or >12.
Hint: Do ... Until "Within Range"

The years picked are good enough to test.
Though it possible to derive a test of leap year "ness" by knowing that a leap year, is any year that can be wholly divisible by 4 (2000, 2004 2008 etc) and also not wholly divisible by 100 (which rules out 2000 from the possibilities.)
Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1642
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: select case construct vb - days in a month

Posted 13 February 2009 - 07:37 PM

Another thing to mention, you have all those years hard code in checking for a leap year, why not just use the IsLeapYear Method. like this

Module Module1
	Sub Main()
		Dim monthnumber As Integer
		Dim year As Integer
		Dim numberofdays As Integer
		Console.WriteLine("Please enter Month number:January=1,February=2 etc.")
		monthnumber = (Console.ReadLine())
		Console.WriteLine("Please enter year:")

		year = Convert.ToBoolean(Console.ReadLine())
		If monthnumber > 1 AndAlso monthnumber < 12 Then
			Select Case monthnumber
				Case 1, 3, 5, 7, 8, 10, 12
					numberofdays = "There are 31 days in this month."
				Case 4, 6, 9, 11
					numberofdays = "There are 30 days in this month."
				Case 2
					If DateTime.IsLeapYear(year) Then
						  numberofdays = 29
					Else
						  numberofdays = 28
					End If
			End Select
		  
			Console.WriteLine("Number of Days In Month is {0}",numberofdays)
		Else
			Console.WriteLine("Please enter a valid month")
		End If		
	End Sub
End Module



Hope that helps :)
Was This Post Helpful? 0
  • +
  • -

#4 kaybear912  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 07-February 09

Re: select case construct vb - days in a month

Posted 13 February 2009 - 08:37 PM

Does that code work for you? I get an error message as soon as I enter the year and press enter.
Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,500
  • Joined: 29-May 08

Re: select case construct vb - days in a month

Posted 13 February 2009 - 08:40 PM

No I didn't test it. Just looked at it.

Surprise bot me and Psychocoder missed this one!
Try changing.
  year = Convert.ToBoolean(Console.ReadLine())


to
 year  = Convert.ToInt16(Console.ReadLine())



Else try adding
Option Explicit


to the beginning of the code.

This post has been edited by AdamSpeight2008: 13 February 2009 - 08:41 PM

Was This Post Helpful? 0
  • +
  • -

#6 kaybear912  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 07-February 09

Re: select case construct vb - days in a month

Posted 13 February 2009 - 08:50 PM

is there another way to write this? I don't remember reading about that yet.
If DateTime.IsLeapYear(year) Then


also - can you explain why you added this...........
   Console.WriteLine("Number of Days In Month is {0}", numberofdays)
		Else
			Console.WriteLine("Please enter a valid month")


I am having trouble knowing what to convert to.........like when you suggested to convert that to int16.....why
Was This Post Helpful? 0
  • +
  • -

#7 kaybear912  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 07-February 09

Re: select case construct vb - days in a month

Posted 13 February 2009 - 09:00 PM

View Postkaybear912, on 13 Feb, 2009 - 07:50 PM, said:

is there another way to write this? I don't remember reading about that yet.
If DateTime.IsLeapYear(year) Then


also - can you explain why you added this...........
   Console.WriteLine("Number of Days In Month is {0}", numberofdays)
		Else
			Console.WriteLine("Please enter a valid month")


I am having trouble knowing what to convert to.........like when you suggested to convert that to int16.....why

but it does seem to have worked!!!!! I just want to understand it!
Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,500
  • Joined: 29-May 08

Re: select case construct vb - days in a month

Posted 13 February 2009 - 10:19 PM

   Console.WriteLine("Number of Days In Month is {0}", numberofdays)
		Else
			Console.WriteLine("Please enter a valid month")


First part.
{0} takes the value of the first argument after the string and insert that value into the string.
Console.WriteLine("{0} says hello to {1}","AdamSpeight2008","kaybear912")


output
AdamSpeight2008 says hello to kaybear912
Second part is what happens if the user enter month of <1 or a month > 12.

This code
If DateTime.IsLeapYear(year) Then


could be rewritten as
If (year Mod 4=0) AndAlso (year Mod 100<>0) Then


Was This Post Helpful? 0
  • +
  • -

#9 kaybear912  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 07-February 09

Re: select case construct vb - days in a month

Posted 14 February 2009 - 09:25 AM

ok, it wasn't working.......only with the month of february.....I have screwed around with it so much that I don't even know where I went wrong or what to change. WOW - this is like greek to me. I need a kindergarden explanation of how to figure this thing out.
This is a program that will determine how many days in a given month.
Module Module1
	Sub Main()
		Dim monthnumber As Integer
		Dim year As Integer
		Dim numberofdays As Integer
		Dim isleapyear As Boolean
		Console.WriteLine("Please enter Month number:January=1,February=2 etc.")
		monthnumber = Convert.ToInt32(Console.ReadLine())
		Console.WriteLine("Please enter year:")
		year = Convert.ToInt16(Console.ReadLine())
		isleapyear = Convert.ToBoolean(Console.ReadLine())
		If monthnumber > 1 AndAlso monthnumber < 12 Then
			Select Case monthnumber
				Case 1, 3, 5, 7, 8, 10, 12
					numberofdays = "There are 31 days in this month."
				Case 4, 6, 9, 11
					numberofdays = "There are 30 days in this month."
				Case 2
					Select Case year
						Case 2004, 2008, 2012, 2016
							numberofdays = 29
						Case Else
							numberofdays = 28
					End Select
			End Select

		End If
	End Sub
End Module



Was This Post Helpful? 0
  • +
  • -

#10 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2271
  • View blog
  • Posts: 9,500
  • Joined: 29-May 08

Re: select case construct vb - days in a month

Posted 14 February 2009 - 09:18 PM

Change
  If monthnumber > 1 AndAlso monthnumber < 12 Then
	  


to
  If monthnumber >= 1 AndAlso monthnumber <= 12 Then
	  


Was This Post Helpful? 0
  • +
  • -

#11 WarMacre  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 03-March 09

Re: select case construct vb - days in a month

Posted 03 March 2009 - 10:31 PM

Are you using VB 6.0, as you have posted in the .NET forum???

Anywho, here's my version. The 'IsLeapYear' is a boolean, so to check if it is true you only need to put 'If IsLeapYear' to evaluate a true statement.

		Dim NumberOfDays As Integer

		Console.WriteLine("Please enter Month number (MM):January=1,February=2 etc.")

		Dim monthNumber as integer= cint(Console.ReadLine())

		Console.WriteLine("Please enter year (YYYY):")

		Dim Year as integer = Cint(Console.ReadLine())

		Dim IsLeapYear As Boolean = DateTime.IsLeapYear(Year)

if Month >= 1 andalso <=12
		Select Case monthNumber
			Case 1, 3, 5, 7, 8, 10, 12
				NumberOfDays = 31
			Case 4, 6, 9, 11
				NumberOfDays = 30
			Case 2
				If IsLeapYear Then
					NumberOfDays = 29
				Else
					NumberOfDays = 28
				End If
		End Select

		console.writline(NumberOfDays)

end if

Was This Post Helpful? 0
  • +
  • -

#12 cdebel2005  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 22-March 09

Re: select case construct vb - days in a month

Posted 22 March 2009 - 08:03 AM

Why don't you simply use this?
DateTime.DaysInMonth(year, month)

Was This Post Helpful? 0
  • +
  • -

#13 dbasnett  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 110
  • View blog
  • Posts: 609
  • Joined: 01-October 08

Re: select case construct vb - days in a month

Posted 22 March 2009 - 09:54 AM

	Sub Main()
		Dim yr As Integer
		Dim mo As Integer
		Dim s As String
		Do
			yr = 0
			Do While yr < 1 OrElse yr > 9999
				Console.WriteLine("Year?")
				s = Console.ReadLine
				Integer.TryParse(s, yr)
			Loop
			mo = 0
			Do While mo < 1 OrElse mo > 12
				Console.WriteLine("Month?")
				s = Console.ReadLine
				Integer.TryParse(s, mo)
			Loop
			Dim dyIM As String = DateTime.DaysInMonth(yr, mo).ToString
			Console.Write(dyIM.ToString)
			If DateTime.IsLeapYear(yr) Then Console.WriteLine("  Leap Year") Else Console.WriteLine("")
			Console.WriteLine("Any letter to continue, Enter to end")
		Loop Until Console.ReadLine = ""
	End Sub
 

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1