Need a bit of help with this code

bradleyman88

New member
Joined
May 8, 2013
Messages
3
Programming Experience
Beginner
Hey all, first time poster here, and a complete newbie at VB.net. I'm supposed to be creating a program that takes the user inputted year in which they were born, and have the program spit back out how old they are and a fact about the year/decade that the user was born in. I have the code mostly done, i'm just stuck on what to do next to get the program to work
Module Module1
    Sub Main()
        Dim YearofBirth As Integer
        Dim age As Integer
        System.Console.WriteLine("This program will find your age, given the year you were born.")
        System.Console.WriteLine("Press any key to continue.")
        System.Console.ReadKey()
        System.Console.WriteLine("Please enter the year in which you were born.")
        YearofBirth = Console.ReadLine()
        Do Until YearofBirth > 0 And YearofBirth < 2014
        Loop
        If YearofBirth > 2013 Or YearofBirth < 0 Then System.Console.WriteLine("Number is not in range and will not be used.")
        System.Console.WriteLine("Please enter the year in which you were born.")
        YearofBirth = Console.ReadLine()
        Do Until age = 2012 - YearofBirth
        Loop
        elseIf age > 114 Then System.Console.WriteLine("You were born in a decade before the 1900's.")
        System.Console.WriteLine("You might be " & age & " years old.")
        System.Console.ReadKey()


        elseIf age > 104 Then System.Console.WriteLine("You were born in the decade beginning with 1900")
        System.Console.WriteLine("You are " & age & " years old.")
        System.Console.WriteLine("The Commonwealth of Australia was created.")
        System.Console.ReadKey()


        elseIf age > 94 Then System.Console.WriteLine("You were born in the decade beginning with 1910")
        System.Console.WriteLine("You are " & age & " years old.")
        System.Console.WriteLine("The S. S. Titanic sinks on her maiden voyage after colliding with an iceberg. 1513 drown.")
        System.Console.ReadKey()


        elseIf age > 84 Then System.Console.WriteLine("You were born in the 1920s")
        System.Console.WriteLine("You are " & age & " years old.")
        System.Console.WriteLine("Refrigerator sales reach 75,000 up from 10,000 in 1920")
        System.Console.ReadKey()


        elseIf age > 74 Then System.Console.WriteLine("You were born in the 1930's")
        System.Console.WriteLine("You are " & age & " years old.")
        System.Console.WriteLine("Scotch tape was invented by 3M engineer Richard Drew.")
        System.Console.ReadKey()


        elseIf age > 64 Then System.Console.WriteLine("You were born in the 1950's")
        System.Console.WriteLine("You are " & age & " years old.")
        System.Console.WriteLine("Les Paul started selling his classic electric guitar.")
        System.Console.ReadKey()


        elseIf age > 44 Then System.Console.WriteLine("You were born in the 1960's")
        System.Console.WriteLine("You are " & age & " years old.")
        System.Console.WriteLine("President John F. Kennedy was assassinated.")
        System.Console.ReadKey()


        elseIf age > 34 Then System.Console.WriteLine("You were born in the 1970's")
        System.Console.WriteLine("You are " & age & " years old.")
        System.Console.WriteLine("The cost of a SuperBowl ad was $125,000")
        System.Console.ReadKey()


        elseIf age > 24 Then System.Console.WriteLine("You were born in the 1980's")
        System.Console.WriteLine("You are " & age & " years old.")
        System.Console.WriteLine("Michael Jackson's Thriller album was released.")
        System.Console.ReadKey()


        elseIf age > 14 Then System.Console.WriteLine("You were born in the 1990's")
        System.Console.WriteLine("You are " & age & " years old.")
        System.Console.WriteLine("The best film Oscar winner was Unforgiven.")
        System.Console.ReadKey()


        elseIf age > 4 Then System.Console.WriteLine("You were born in the decade beginning with the year 2000")
        System.Console.WriteLine("You are " & age & " years old.")
        System.Console.WriteLine("Steve Fossett finished his non-stop trip around the world via balloon.")
        System.Console.ReadKey()


        elseIf age < 4 Then System.Console.WriteLine("You were born in the current decade!")
        System.Console.WriteLine("You are " & age & " years old.")
        System.Console.WriteLine("Barack Obama is the President.")
        System.Console.ReadKey()
        End if
    End Sub


End Module

There is the code that i have done so far, but i'm also attaching the .txt to the post if the formatting isn't correct
 

Attachments

  • final project4.txt
    4.1 KB · Views: 11
Last edited by a moderator:
I have added formatting tags to your code snippet. Please do so for us in future.

You have told us what you're trying to do and how you're trying to do it, which is good, but you haven't actually told us what the problem is. You say:
i'm just stuck on what to do next to get the program to work
but you don't tell us in exactly what way it's not working. Exactly how does the actual behaviour differ from the expected. It's much easier for us to solve a problem if we know what we're looking for.
 
Sorry, let me clarify. For debugging, each and every one of the ElseIf's say that there is no If that it is connected to, yet there is an if before all of them, so i'm unsure of why that isnt working. If i remove the elseif's and replace them with if's, the program runs, but it doesnt display the facts that i need to display. And as i said before, i was unsure of how to edit the snippet or what to do with it,
 
I seem to have solved my problem, I hadn't realized that the system.console.writeline line could not be directly after the Then command, and after i moved it, all my exceptions went away. Thanks for the consideration!
 
The reason that occurs is that you have the action for the first If statement on the same line. This code of yours:
VB.NET:
If YearofBirth > 2013 Or YearofBirth < 0 Then System.Console.WriteLine("Number is not in range and will not be used.")
is actually equivalent to this:
VB.NET:
If YearofBirth > 2013 Or YearofBirth < 0 Then
    System.Console.WriteLine("Number is not in range and will not be used.")
End If
That means that everything after that line in your code is not associated with that If statement in any way. You've got similar issues in the rest of the code too, with other statements appearing immediately following the Then keyword. I pretty much NEVER put anything after the Then keyword. The only time you can do so is for the simplest of cases, i.e. there is one line of code to execute if the condition is True and no Else at all. The only time I would ever do that is when I have a number of simple If statements to put together and so this:
VB.NET:
If someThing1 Then DoSomethingElse1()
If someThing2 Then DoSomethingElse2()
If someThing3 Then DoSomethingElse3()
is easier to read than this:
VB.NET:
If someThing1 Then
    DoSomethingElse1()
End If

If someThing2 Then
    DoSomethingElse2()
End If

If someThing3 Then
    DoSomethingElse3()
End If
Even if the If statement is very simple, I still write it out the long way if there's only one of them.
 
Back
Top