Reputation: 75
Here's all my code
Module Module1
Sub Main()
Dim yob As Integer
System.Console.WriteLine("If you would as be kind as to input your year of birth then I will tell you how old you are")
loops:
Try
yob = Int32.Parse(Console.ReadLine())
Catch ex As SystemException
Console.WriteLine("that is not a number, try again.")
GoTo loops
Finally
End Try
Do Until yob > 0 And yob < DateTime.Today.Year
Loop
If yob < 0 Or yob > DateTime.Today.Year Then
Console.WriteLine("You entered a number that is either zero or a date in the future.")
Console.WriteLine("You can't be born in the future. C'mon son")
Console.WriteLine("Try again")
End If
Dim Age = DateTime.Today.Year - yob
Console.WriteLine("You are " & Age & " years old")
If Age > 100 Then
Console.WriteLine("You are were born over a 100 years ago and little is known about that time since there was no Facebook, Twitter or Instagram for people to log their every")
Console.WriteLine(" thought, action, emotion, or take pictures of their food before this time")
Console.ReadKey()
End If
If Age > 90 Then
Console.WriteLine("You were born in the 20s")
Console.WriteLine("In this decade Halloween was born. The first Halloween celebration in America took place in Anoka, Minnesota in 1921. ")
Console.ReadKey()
End If
End Sub
End Module
Until I added a goto in my try catch block my loop would run fine. Now it doesn't and if you input a year bigger than 2014 It just sits their. The other problem I have is that my 2nd If doesn't work. If you put in a year that makes your age bigger than 90 It doesn't do anything and the program closes without performing the If statements. Any Ideas on what's wrong?
Upvotes: 0
Views: 87
Reputation: 216273
There are two problems in your code. (Well, three if I count also the goto
)
The first one is the empty do while
that, if you type a value bigger than the current year, will loop forever as pointed in the comments above. The second problem is the lacking of an else if
between your two outputs. If I have more than 100 years then I have also more than 90 years and you write both statements.
The first problem is fixed removing all the try/catch block and the Do while loop with
While True
if Int32.TryParse(Console.ReadLine(), yob) then
if yob > 0 And yob < DateTime.Today.Year then
Exit While
else
Console.Writeline("Enter a number between 0 and " & DateTime.ToDay.Year)
End if
else
Console.Writeline("This is not a number")
End if
End While
Notice that TryParse is preferable to the simple Parse because if the input is not a number it will return false and it doesn't throw an exception.
The second problem is simply fixed adding the appropriate else if
(and use Console.Readline to be sure to exit when the user presses enter)
If Age > 100 Then
.....
Console.ReadLine()
else If Age > 90 Then
....
Console.ReadLine()
End If
However keep in mind that calculating the age of a person is not a simple task as it seems.
Look at this question
Upvotes: 1
Reputation: 700232
Wow, a Goto
. Not everyday that you see one of those, and for good reason. You shouldn't use it. There are some situations where it's useful, but you are not likely to actually encounter one of them, ever.
The reason that the code stops for a year out of range is that you have an empty loop where you check the year. As the variable can't change inside the loop, the loop will never end.
You should use the loop around the input instead of after it, and do all the checking inside the loop. You can use the TryParse
method instead of letting the code throw an exception, it's usually better to check for the condition that would cause an error instead of waiting for it to happen:
Dim yob As Integer
System.Console.WriteLine("If you would as be kind as to input your year of birth then I will tell you how old you are")
Dim inputting As Boolean = True
Do While inputting
Dim input As String = Console.ReadLine()
If Int32.TryParse(input, yob) Then
If yob <= 0 Or yob > DateTime.Today.Year Then
Console.WriteLine("You entered a number that is either zero or a date in the future.")
Console.WriteLine("You can't be born in the future. C'mon son")
Console.WriteLine("Try again")
Else
inputting = False
End If
Else
Console.WriteLine("that is not a number, try again.")
End If
Loop
When I test with a year that makes me 94, the second If
statement works fine. However, it only checks the lower bound, so if I enter a year that makes me 150 years old, it still says that I was born in the twenties.
Also, being between 90 and 100 doesn't mean that you were born in the twenties, but being between 85 and 95 does. You might want to use the actual birth year in that condition instead of the age:
If yob >= 1920 And yob <= 1929 Then
Side note: Only the birth year is not enough to accurately determine the age. If I was born 1994 it's not certain that I am 20 years old, if I haven't had my birthday yet this year, I'm still 19.
Upvotes: 1
Reputation: 8475
The code you want to loop has to be in the Do
loop.
Do
Try
yob = Int32.Parse(Console.ReadLine())
Catch ex As SystemException
Console.WriteLine("that is not a number, try again.")
GoTo loops
Finally
End Try
Loop Until yob > 0 And yob < DateTime.Today.Year
Upvotes: 2