Results 1 to 21 of 21

Thread: Loop

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    13

    Loop

    I want to put a loop on this program
    Code:
    Module Module1
        Class Arithmetical
            Public Function Add(ByVal A As Integer, ByVal B As Integer, ByVal C As Integer)
                Return A + B + C
            End Function
            Public Function Mult(ByVal A As Integer, ByVal B As Integer, ByVal C As Integer)
                Return A * B * C
            End Function
        End Class
        Sub Main()
    
            Dim obj As New Arithmetical
            Dim A, B, C, Sum, Product As Integer
            Dim Repeater As String
            Console.WriteLine("Please enter a number value.")
            A = Console.ReadLine
            B = Console.ReadLine
            C = Console.ReadLine
            Sum = (obj.Add(A, B, C))
            Console.WriteLine("Here is the result when the numbers are added:")
            Console.WriteLine(Sum)
            Product = (obj.Mult(A, B, C))
            Console.WriteLine("Here is the result when the numbers are divided:")
            Console.WriteLine(Product)
    
            Console.WriteLine("Would you like to run this program again? Y/N")
            Repeater = Console.ReadLine
            If Repeater = "Y" Then
    
    
    
            End If
    
    
            Console.ReadKey()
    
    
    
        End Sub
    
    
    End Module
    Where it says If repeater = Y I want it to go back to the top of the program.
    How would I go about doing this?

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Loop

    Code:
    Module Module1
             Public Repeater As String
    
        Class Arithmetical
    
            Public Function Add(ByVal A As Integer, ByVal B As Integer, ByVal C As Integer)
                Return A + B + C
            End Function
    
            Public Function Mult(ByVal A As Integer, ByVal B As Integer, ByVal C As Integer)
                Return A * B * C
            End Function
        End Class
    
    Public Function DoMath()
            Dim obj As New Arithmetical
            Dim A, B, C, Sum, Product As Integer
            Console.WriteLine("Please enter a number value.")
            A = Console.ReadLine
            B = Console.ReadLine
            C = Console.ReadLine
            Sum = (obj.Add(A, B, C))
            Console.WriteLine("Here is the result when the numbers are added:")
            Console.WriteLine(Sum)
            Product = (obj.Mult(A, B, C))
            Console.WriteLine("Here is the result when the numbers are divided:")
            Console.WriteLine(Product)
    
            Console.WriteLine("Would you like to run this program again? Y/N")
            Repeater = Console.ReadLine
    End Function
    
    Sub Main()
        DoMath()
        If Repeater = "Y" Then
           DoMath()
        End If
    End Sub

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    13

    Re: Loop

    Erm, that code doesn't seem to work for me. It's saying that DoMath is not declared?

  4. #4
    Hyperactive Member
    Join Date
    Dec 2000
    Location
    Sydney
    Posts
    311

    Re: Loop

    Do you have this line in your code?
    vb Code:
    1. Public Function DoMath()
    Why do today what you can tomorrow...

  5. #5
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Loop

    Did you remember to add End Module?

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    13

    Re: Loop

    Okay, I realized the problem-- I was ending the class AFTER the DoMath Function. However, now when I am debugging, I attempt to run the program again but after I hit Y and enter, the cursor goes down a line, and upon hitting enter again, ends?

  7. #7
    Hyperactive Member
    Join Date
    Dec 2000
    Location
    Sydney
    Posts
    311

    Re: Loop

    Try this:

    vb Code:
    1. Sub Main()
    2.     DoMath()
    3.     If Repeater.ToUpper = "Y" Then
    4.        DoMath()
    5.     End If
    6. End Sub

    Also look at this line: Console.WriteLine("Here is the result when the numbers are divided:")
    You're multiplying the numbers, not dividing them...
    Why do today what you can tomorrow...

  8. #8

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    13

    Re: Loop

    Now it says NullReferenceException was unhandled, when and it points to The Then after If Repeater.toUpper = Y.

  9. #9
    Hyperactive Member
    Join Date
    Dec 2000
    Location
    Sydney
    Posts
    311

    Re: Loop

    Works for me...Post your code again as it is now.

    And also, assuming you want to run this until the user does not press 'N', your code should be:
    vb Code:
    1. Sub Main()
    2.         DoMath()
    3.         Do While Repeater.ToUpper = "Y"
    4.             DoMath()
    5.         Loop
    6.     End Sub
    Why do today what you can tomorrow...

  10. #10
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Loop

    Or perhaps:
    vb Code:
    1. Sub Main()
    2.     Do
    3.         DoMath()
    4.     Loop While Repeater.ToUpper = "Y"
    5. End Sub

    ?

  11. #11

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    13

    Re: Loop

    http://tinypic.com/r/28vboma/7

    so thats what its saying..

  12. #12

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    13

    Re: Loop

    Same issue again, even with the new code.

  13. #13

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    13

    Re: Loop

    Anyway, if no one can help me with that, I was wondering if I could get some help with exception handling, like a try-catch to make sure the user inputs numbers instead of letters.

  14. #14
    Hyperactive Member
    Join Date
    Dec 2000
    Location
    Sydney
    Posts
    311

    Re: Loop

    I tried to help...See post #9.
    Why do today what you can tomorrow...

  15. #15

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    13

    Re: Loop

    Sorry, did the tinypic not get the full code?

  16. #16
    Hyperactive Member
    Join Date
    Dec 2000
    Location
    Sydney
    Posts
    311

    Re: Loop

    Click the link, you tell me.
    Why do today what you can tomorrow...

  17. #17

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    13

    Re: Loop

    Code:
    Module Module1
        Public Repeater As String
        Class Arithmetical
            Public Function Add(ByVal A As Integer, ByVal B As Integer, ByVal C As Integer)
                Return A + B + C
            End Function
            Public Function Mult(ByVal A As Integer, ByVal B As Integer, ByVal C As Integer)
                Return A * B * C
            End Function
        End Class
    
        Public Function DoMath()
    
            Dim obj As New Arithmetical
            Dim A, B, C, Sum, Product As Integer
            Dim Repeater As String
            Console.WriteLine("Please enter three values.")
            A = Console.ReadLine
            B = Console.ReadLine
            C = Console.ReadLine
            Sum = (obj.Add(A, B, C))
            Console.WriteLine("Here is the result when the numbers are added:")
            Console.WriteLine(Sum)
            Product = (obj.Mult(A, B, C))
            Console.WriteLine("Here is the result when the numbers are multiplied:")
            Console.WriteLine(Product)
    
            Console.WriteLine("Would you like to run this program again? Y/N")
            Repeater = Console.ReadLine
    
        End Function
    
    
    
       Sub Main()
            DoMath()
            Do While Repeater.ToUpper = "Y"
                DoMath()
            Loop
        End Sub
    
    
    End Module
    
    I tried the version posted both by you and by Evil Giraffe. The End Function after the DoMath function has something wrong with it... although I can't tell what.

  18. #18
    Hyperactive Member
    Join Date
    Dec 2000
    Location
    Sydney
    Posts
    311

    Re: Loop

    Remove this line in the DoMath function:

    vb Code:
    1. Dim Repeater As String

    And take Evil_Giraffe's advice:

    vb Code:
    1. Sub Main()
    2.         Do
    3.             DoMath()
    4.         Loop While Repeater.ToUpper = "Y"
    5.     End Sub
    Why do today what you can tomorrow...

  19. #19

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    13

    Re: Loop

    That did it. Thank you.

  20. #20

    Thread Starter
    New Member
    Join Date
    Jan 2011
    Posts
    13

    Re: Loop

    How would i go about adding a Catch Try Finally so no users input a letter as opposed to a number?

  21. #21
    Hyperactive Member
    Join Date
    Dec 2000
    Location
    Sydney
    Posts
    311

    Re: Loop

    Test to see if A, B, and C are numeric. E.G.
    vb Code:
    1. If Not IsNumeric(A) Then
    2.                 'Do Something
    3.             End If

    Also you should turn on 'Option Explicit' and 'Option Strict' within VB, Don't know what I mean? Search for them online.

    And your function returns nothing, which is why it's underlined in green.
    Why do today what you can tomorrow...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width