In my VB school project we are doing an area calculator. this is my current program. When i put a break point at the "enter side width" (line 45) it says input is 0 but according to my function labelled "GetVal" it should be readline(). The rest of the function parts aren't using the GetVal because i want to make it work on the square first.
Imports System.Console
Module Module1
Private Function SquareArea(ByVal Length As Single, ByVal Width As Single) As Single
Return Length * Width
End Function
Private Function CircleArea(ByVal Radius As Single) As Single
Return 3.14159 * (Radius * Radius)
End Function
Private Function TriArea(ByVal Base As Single, ByVal Height As Single) As Single
Return (Base * Height) / 2
End Function
Private Function GetVal(ByVal Input As Single) As Single
Input = ReadLine()
Return Input
End Function
Sub Main()
Dim Length, Width, answer, Radius, Base, Height, Input As Single
Dim Validinput As Integer
Dim Choice As String
Do
WriteLine("Press the nunmpad for the area you want to find")
WriteLine("1 for Rectangle/Square, 2 for Circle, 3 for Triangle")
Do
Validinput = True
Choice = ReadKey(True).Key
If Choice <> ConsoleKey.NumPad1 And Choice <> ConsoleKey.NumPad2 And Choice <> ConsoleKey.NumPad3 Then Validinput = False
Loop Until Validinput
'Square/Rectangle'
If Choice = ConsoleKey.NumPad1 Then
WriteLine("Enter Side Length")
Do
Validinput = True
Try
GetVal(Input = Length)
Input = Length
Catch ex As Exception
WriteLine("Error, Enter a number")
Validinput = False
End Try
Loop Until Validinput = True
WriteLine("Enter Side Width")
Do
Validinput = True
Try
GetVal(Input = Width)
Input = Width
Catch ex As Exception
WriteLine("Please enter a number")
Validinput = False
End Try
Loop Until Validinput = True
WriteLine(SquareArea(Length, Width))
End If
'Circle'
If Choice = ConsoleKey.NumPad2 Then
WriteLine("Enter Radius")
Do
Validinput = True
Try
Radius = ReadLine()
WriteLine(CircleArea(Radius))
Catch ex As Exception
WriteLine("Please Enter a number")
Validinput = False
End Try
Loop Until Validinput = True
End If
'Triangle'
If Choice = ConsoleKey.NumPad3 Then
WriteLine("Enter Base Length")
Do
Validinput = True
Try
Base = ReadLine()
Catch ex As Exception
WriteLine("Please Enter a number")
Validinput = False
End Try
Loop Until Validinput
WriteLine("Enter Height Size")
Do
Validinput = True
Try
Height = ReadLine()
Catch ex As Exception
WriteLine("Please Enter a number")
Validinput = False
End Try
Loop Until Validinput
WriteLine(TriArea(Base, Height))
End If
WriteLine("")
Loop
End Sub
End Module
------------Edit------------------
My final working code if anyone has had the same problem. I know it's not pretty, but it does it's job:
Imports System.Console
Module Module1
Private Function SquareArea(ByVal Length As Single, ByVal Width As Single) As Single
Return Length * Width
End Function
Private Function CircleArea(ByVal Radius As Single) As Single
Return Math.PI * (Radius * Radius)
End Function
Private Function TriArea(ByVal Base As Single, ByVal Height As Single) As Single
Return (Base * Height) / 2
End Function
Private Function GetVal(ByVal Input As Single) As Short
Dim ValueOK As Boolean
Do
ValueOK = True
Try
Input = Readline()
Catch ex As Exception
ValueOK = False
WriteLine("Error, please enter a number")
End Try
Loop Until ValueOK
Return Input
End Function
Sub Main()
Dim Length, Width, Radius, PI, Base, Height, Input As Short
Dim Validinput As Integer
Dim Choice As String
Do
WriteLine("Press the nunmpad for the area you want to find")
WriteLine("1 for Rectangle/Square, 2 for Circle, 3 for Triangle")
Do
Validinput = True
Choice = ReadKey(True).Key
If Choice <> ConsoleKey.NumPad1 And Choice <> ConsoleKey.NumPad2 And Choice <> ConsoleKey.NumPad3 Then Validinput = False
Loop Until Validinput
'Square/Rectangle'
If Choice = ConsoleKey.NumPad1 Then
WriteLine("Enter Side Length")
Validinput = True
Length = GetVal(123456.0)
Input = Length
WriteLine("Enter Side Width")
Validinput = True
Width = GetVal(123456.0)
Input = Width
WriteLine(SquareArea(Length, Width))
End If
'Circle'
If Choice = ConsoleKey.NumPad2 Then
WriteLine("Enter Radius")
Validinput = True
Radius = GetVal(123456.0)
Input = Radius
WriteLine(CircleArea(Radius))
End If
'Triangle'
If Choice = ConsoleKey.NumPad3 Then
WriteLine("Enter Base Length")
Validinput = True
Base = GetVal(123456.0)
Input = Base
WriteLine("Enter Height Size")
Validinput = True
Height = GetVal(123456.0)
Input = Height
WriteLine(TriArea(Base, Height))
End If
WriteLine("")
Loop
End Sub
End Module