0

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
6
  • 1
    Post the function and any other relevant code. We can't help you with something we can't see. Commented Nov 19, 2013 at 20:43
  • 1
    On line 45? This is certainly important. I will think about what might be the problem and will come back to you ASAP. Commented Nov 19, 2013 at 20:43
  • Cheers and i have posted the code now sorry Commented Nov 19, 2013 at 20:48
  • After a quick look at your code (and at your question); I guess that the best advice I can give you is: try (much) harder in this class of yours before coming to SO; there is not just a bit which requires some improvement. The structure is bad but also denotes that your basic knowledge is not too solid. Take more lessons and don't expect to get results so quickly (remember: SO is for knowledgeable programmers), taking things with calm is the way to get proper knowledge. Commented Nov 19, 2013 at 20:54
  • It was method 2 in Douglas Barbin's Post that worked for me. Sorry for my code being all over the place i have only been doing this subject for 2 months at GCSE and I saw that when I signed up, there was a complete beginner part. Commented Nov 19, 2013 at 21:03

1 Answer 1

1

First, if you had Option Strict On this wouldn't even compile. Part of your problem is this:

GetVal(Input = Length)

Here, you are basically calling GetVal(True) or GetVal(False) since Input = Length is a comparison and evaluates to either true or false.

Furthermore, you are calling a Function that returns a value, but doing nothing with the return value. You have 2 options here:

  1. Change your functions to ByRef instead of ByVal, which will allow you to overwrite the variable that you are passing in.

  2. Change GetVal(Input = Length) etc. to Length = GetVal(123456.0). In fact, the way you have your function written, it doesn't even matter what number you pass to GetVal because it gets immediately overwritten inside of the function anyways. You could simply alter GetVal to not take in any number at all, and it would make no difference.

Solution #2 is the better solution, and the proper way to do this.

You need something like this:

Option Strict On

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.0
    End Function

    Private Function GetVal() As Single
        Dim userInput As String = ReadLine()
            Return Convert.ToSingle(userInput)
        End Try
    End Function

    Sub Main()
        Dim Length As Single
        Dim Width As Single
        Dim answer As Single
        Dim Radius As Single
        Dim Base As Single
        Dim Height 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
                        Length = GetVal()
                    Catch ex As Exception
                        WriteLine("Error, Enter a number")
                        Validinput = False
                    End Try
                Loop Until Validinput

                WriteLine("Enter Side Width")
                Do
                    Validinput = True
                    Try
                        Width = GetVal()
                    Catch ex As Exception
                        WriteLine("Please enter a number")
                        Validinput = False
                    End Try
                Loop Until Validinput
                WriteLine(SquareArea(Length, Width))
            End If
            'Circle'
            If Choice = ConsoleKey.NumPad2 Then
                WriteLine("Enter Radius")
                Do
                    Validinput = True
                    Try
                        Radius = GetVal()
                        WriteLine(CircleArea(Radius))
                    Catch ex As Exception
                        WriteLine("Please Enter a number")
                        Validinput = False
                    End Try
                Loop Until Validinput
            End If
            'Triangle'
            If Choice = ConsoleKey.NumPad3 Then
                WriteLine("Enter Base Length")
                Do
                    Validinput = True
                    Try
                        Base = GetVal()
                    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 = GetVal()
                    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

Also, this would be a situation to use a Select Case statement instead of multiple If statements.

Sign up to request clarification or add additional context in comments.

2 Comments

@varocarbas I was getting to that :)
Sorry for interrupting, lots to write here ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.