What's new

# Help with HW problem

S

#### Sigeli

##### Enthusiast
Hey everyone, I am in my first semester of programming and I have a quick question. I am making a console application. Here is the problem

1. Write a Visual Basic Console Application which inputs a month number and a year. The month number would be 1 for January, 2 for February, 3 for March, 4 for April, etc. Based on the month number and the year entered, your program should determine and then output the number of days belonging to that month. For example, if 4 is entered for the month number, your program should output “There are 30 days in this month.” See Example 4-19 on page 134 of your book for help with this exercise. Use a select case to drive the conditional logic. The select case statement should use the value of month and year to determine the number of days in the month. Take extra care with month number 2. If the month is 2 your program should use a second select case statement based on year to determine the number of days in the month.

I have this so far for my code

Module Module1

Sub Main()
Dim month As String
Dim days As Integer
Dim numberOfDays As Integer
Dim year As Integer
'Prompt user to enter input

Select Case month
Case 4, 6, 9, 11
numberOfDays = 30
Console.WriteLine("There are 30 days in this month.")
Case 1, 3, 5, 7, 8, 10, 12
numberOfDays = 31
Console.WriteLine("There are 31 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

My question is; How do I read the users input after entering the month to determine what number it gets?

Last edited:
Z

#### Z61

##### Some times our saints are sinners
Retired
My question is; How do I read the users input after entering the month to determine what number it gets?
What you need to do is declare a value to hold the input.
I believe it would be:
Code:
``Dim input as string = Console.ReadLine()``

S

#### Sigeli

##### Enthusiast
Code:
``````Module Module1

Sub Main()
Dim month As Integer
Dim numberOfDays As Integer
Dim year As Integer
'Prompt user to enter input
Console.WriteLine("Please enter a month Jan = 1, Feb = 2, etc")
'Prompt user to enter year

If month > 1 AndAlso month <= 12 Then

Select Case month
Case 4, 6, 9, 11
numberOfDays = 30
Console.WriteLine("There are 30 days in this month")

Case 1, 3, 5, 7, 8, 10, 12
numberOfDays = 31
Console.WriteLine("There are 31 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``````
T

This is what I have now, I feel like I am getting closer to the end result.

V

#### Visual Studio

##### The Original Shiba Inu
Hey everyone, I am in my first semester of programming and I have a quick question. I am making a console application. Here is the problem

1. Write a Visual Basic Console Application which inputs a month number and a year. The month number would be 1 for January, 2 for February, 3 for March, 4 for April, etc. Based on the month number and the year entered, your program should determine and then output the number of days belonging to that month. For example, if 4 is entered for the month number, your program should output “There are 30 days in this month.” See Example 4-19 on page 134 of your book for help with this exercise. Use a select case to drive the conditional logic. The select case statement should use the value of month and year to determine the number of days in the month. Take extra care with month number 2. If the month is 2 your program should use a second select case statement based on year to determine the number of days in the month.

I have this so far for my code

Module Module1

Sub Main()
Dim month As String
Dim days As Integer
Dim numberOfDays As Integer
Dim year As Integer
'Prompt user to enter input

Select Case month
Case 4, 6, 9, 11
numberOfDays = 30
Console.WriteLine("There are 30 days in this month.")
Case 1, 3, 5, 7, 8, 10, 12
numberOfDays = 31
Console.WriteLine("There are 31 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

My question is; How do I read the users input after entering the month to determine what number it gets?
This is what I have:

Fake Language:
Code:
``````Console.Write("Please enter a month (Jan = 1, Feb = 2, etc): ")
Dim month As Integer = Integer.Parse(Console.ReadLine())

Dim year As Integer = Integer.Parse(Console.ReadLine())

Dim number_of_days As Integer = 0
If month >= 1 AndAlso month <= 12 Then
If New Integer() {4, 6, 9, 11}.Contains(month) Then
number_of_days = 30
Console.WriteLine("There are 30 days in this month.")
ElseIf New Integer() {1, 3, 5, 7, 8, 10, 12}.Contains(month) Then
number_of_days = 31
Console.WriteLine("There are 31 days in this month.")
ElseIf month = 2 Then
If year Mod 4 = 0 Then
number_of_days = 29
Else
number_of_days = 28
End If
End If
End If

Console.WriteLine("Number of days in the month: " + number_of_days)

Real Language:
Code:
``````Console.Write("Please enter a month (Jan = 1, Feb = 2, etc): ");

int number_of_days = 0;
if(month >= 1 && month <= 12)
{
if(new int[] { 4, 6, 9, 11 }.Contains(month))
{
number_of_days = 30;
Console.WriteLine("There are 30 days in this month.");
}
else if(new int[] { 1, 3, 5, 7, 8, 10, 12 }.Contains(month))
{
number_of_days = 31;
Console.WriteLine("There are 31 days in this month.");
}
else if(month == 2)
{
if(year % 4 == 0)
{
number_of_days = 29;
}
else
{
number_of_days = 28;
}
}
}

Console.WriteLine("Number of days in the month: " + number_of_days);

S

#### Sigeli

##### Enthusiast
This is what I have:

Fake Language:
Code:
``````Console.Write("Please enter a month (Jan = 1, Feb = 2, etc): ")
Dim month As Integer = Integer.Parse(Console.ReadLine())

Dim year As Integer = Integer.Parse(Console.ReadLine())

Dim number_of_days As Integer = 0
If month >= 1 AndAlso month <= 12 Then
If New Integer() {4, 6, 9, 11}.Contains(month) Then
number_of_days = 30
Console.WriteLine("There are 30 days in this month.")
ElseIf New Integer() {1, 3, 5, 7, 8, 10, 12}.Contains(month) Then
number_of_days = 31
Console.WriteLine("There are 31 days in this month.")
ElseIf month = 2 Then
If year Mod 4 = 0 Then
number_of_days = 29
Else
number_of_days = 28
End If
End If
End If

Console.WriteLine("Number of days in the month: " + number_of_days)

Real Language:
Code:
``````Console.Write("Please enter a month (Jan = 1, Feb = 2, etc): ");

int number_of_days = 0;
if(month >= 1 && month <= 12)
{
if(new int[] { 4, 6, 9, 11 }.Contains(month))
{
number_of_days = 30;
Console.WriteLine("There are 30 days in this month.");
}
else if(new int[] { 1, 3, 5, 7, 8, 10, 12 }.Contains(month))
{
number_of_days = 31;
Console.WriteLine("There are 31 days in this month.");
}
else if(month == 2)
{
if(year % 4 == 0)
{
number_of_days = 29;
}
else
{
number_of_days = 28;
}
}
}

Console.WriteLine("Number of days in the month: " + number_of_days);

I appreciate you helping me out This would work but we are required to do a case select, not an If/Else statement

S

#### Sigeli

##### Enthusiast
Code:
``````Module Module1

Sub Main()
Dim month As Integer
Dim numberOfDays As Integer
Dim year As Integer
'Prompt user to enter input
Console.WriteLine("Please enter a month Jan = 1, Feb = 2, etc")
'Prompt user to enter year
If month >= 1 AndAlso month <= 12 Then
Select Case month
Case 4, 6, 9, 11
numberOfDays = 30
Console.WriteLine("There are 30 days in this month")

Case 1, 3, 5, 7, 8, 10, 12
numberOfDays = 31
Console.WriteLine("There are 31 days in this month")
Case 2
Select Case year
Case 2004, 2008, 2012, 2016
numberOfDays = 29
Console.WriteLine("There are 29 days in this month")
Case Else
numberOfDays = 28
Console.WriteLine("There are 28 days in this month")

End Select
End Select
End If
Console.WriteLine("Number of days in this month: " + month)
End Sub

End Module``````

Okay so I ended up coming up with this. I got it to work but I am getting an error Invalid Cast Exception near the end of the code, any suggestions on what I could do?

V

#### Visual Studio

##### The Original Shiba Inu
I appreciate you helping me out This would work but we are required to do a case select, not an If/Else statement
You can't have multi-conditional case statements in C# and I just wrote it in C# and translated to VB. Also, notice that I used modulo on the year.

V

#### Visual Studio

##### The Original Shiba Inu
Code:
``````Module Module1

Sub Main()
Dim month As Integer
Dim numberOfDays As Integer
Dim year As Integer
'Prompt user to enter input
Console.WriteLine("Please enter a month Jan = 1, Feb = 2, etc")
'Prompt user to enter year
If month >= 1 AndAlso month <= 12 Then
Select Case month
Case 4, 6, 9, 11
numberOfDays = 30
Console.WriteLine("There are 30 days in this month")

Case 1, 3, 5, 7, 8, 10, 12
numberOfDays = 31
Console.WriteLine("There are 31 days in this month")
Case 2
Select Case year
Case 2004, 2008, 2012, 2016
numberOfDays = 29
Console.WriteLine("There are 29 days in this month")
Case Else
numberOfDays = 28
Console.WriteLine("There are 28 days in this month")

End Select
End Select
End If
Console.WriteLine("Number of days in this month: " + month)
End Sub

End Module``````

Okay so I ended up coming up with this. I got it to work but I am getting an error Invalid Cast Exception near the end of the code, any suggestions on what I could do?
Where exactly is the error showing up in VS?