What's new

Help with HW problem

S

Sigeli

Enthusiast
Messages
42
Reaction score
10
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
Console.WriteLine("Please enter a month")
'Read in users input
month = Console.ReadLine()

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
Messages
5,476
Reaction score
3,426
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
Messages
42
Reaction score
10
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")
        'Read in users input
        month = Convert.ToInt32(Console.ReadLine())
        'Prompt user to enter year
        Console.WriteLine("Please enter a year")
        year = Convert.ToInt16(Console.ReadLine())

        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
Messages
2,758
Reaction score
1,486
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
Console.WriteLine("Please enter a month")
'Read in users input
month = Console.ReadLine()

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())

Console.Write("Please enter a year: ")
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)

Console.ReadKey()

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

Console.Write("Please enter a year: ");
int year = int.Parse(Console.ReadLine());

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);

Console.ReadKey();
 
S

Sigeli

Enthusiast
Messages
42
Reaction score
10
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())

Console.Write("Please enter a year: ")
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)

Console.ReadKey()

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

Console.Write("Please enter a year: ");
int year = int.Parse(Console.ReadLine());

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);

Console.ReadKey();
I appreciate you helping me out :smile: This would work but we are required to do a case select, not an If/Else statement
 
S

Sigeli

Enthusiast
Messages
42
Reaction score
10
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")
        'Read in users input
        month = Convert.ToInt32(Console.ReadLine())
        'Prompt user to enter year
        Console.WriteLine("Please enter a year")
        year = Convert.ToInt16(Console.ReadLine())
        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
Messages
2,758
Reaction score
1,486
I appreciate you helping me out :smile: 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
Messages
2,758
Reaction score
1,486
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")
        'Read in users input
        month = Convert.ToInt32(Console.ReadLine())
        'Prompt user to enter year
        Console.WriteLine("Please enter a year")
        year = Convert.ToInt16(Console.ReadLine())
        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?
 
Top Bottom