|
-
Jan 23rd, 2011, 06:17 PM
#1
Thread Starter
New Member
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?
-
Jan 23rd, 2011, 06:34 PM
#2
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
-
Jan 23rd, 2011, 07:08 PM
#3
Thread Starter
New Member
Re: Loop
Erm, that code doesn't seem to work for me. It's saying that DoMath is not declared?
-
Jan 23rd, 2011, 07:37 PM
#4
Hyperactive Member
Re: Loop
Do you have this line in your code?
Why do today what you can tomorrow...
-
Jan 23rd, 2011, 07:43 PM
#5
Re: Loop
Did you remember to add End Module?
-
Jan 23rd, 2011, 08:52 PM
#6
Thread Starter
New Member
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?
-
Jan 23rd, 2011, 08:56 PM
#7
Hyperactive Member
Re: Loop
Try this:
vb Code:
Sub Main()
DoMath()
If Repeater.ToUpper = "Y" Then
DoMath()
End If
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...
-
Jan 23rd, 2011, 09:10 PM
#8
Thread Starter
New Member
Re: Loop
Now it says NullReferenceException was unhandled, when and it points to The Then after If Repeater.toUpper = Y.
-
Jan 23rd, 2011, 09:19 PM
#9
Hyperactive Member
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:
Sub Main()
DoMath()
Do While Repeater.ToUpper = "Y"
DoMath()
Loop
End Sub
Why do today what you can tomorrow...
-
Jan 23rd, 2011, 09:25 PM
#10
Re: Loop
Or perhaps:
vb Code:
Sub Main()
Do
DoMath()
Loop While Repeater.ToUpper = "Y"
End Sub
?
-
Jan 23rd, 2011, 09:32 PM
#11
Thread Starter
New Member
-
Jan 23rd, 2011, 09:38 PM
#12
Thread Starter
New Member
Re: Loop
Same issue again, even with the new code.
-
Jan 23rd, 2011, 10:34 PM
#13
Thread Starter
New Member
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.
-
Jan 23rd, 2011, 10:37 PM
#14
Hyperactive Member
Re: Loop
I tried to help...See post #9.
Why do today what you can tomorrow...
-
Jan 23rd, 2011, 10:45 PM
#15
Thread Starter
New Member
Re: Loop
Sorry, did the tinypic not get the full code?
-
Jan 23rd, 2011, 10:48 PM
#16
Hyperactive Member
Re: Loop
Click the link, you tell me.
Why do today what you can tomorrow...
-
Jan 23rd, 2011, 10:53 PM
#17
Thread Starter
New Member
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.
-
Jan 23rd, 2011, 11:02 PM
#18
Hyperactive Member
Re: Loop
Remove this line in the DoMath function:
And take Evil_Giraffe's advice:
vb Code:
Sub Main()
Do
DoMath()
Loop While Repeater.ToUpper = "Y"
End Sub
Why do today what you can tomorrow...
-
Jan 23rd, 2011, 11:12 PM
#19
Thread Starter
New Member
-
Jan 24th, 2011, 12:01 AM
#20
Thread Starter
New Member
Re: Loop
How would i go about adding a Catch Try Finally so no users input a letter as opposed to a number?
-
Jan 24th, 2011, 03:26 AM
#21
Hyperactive Member
Re: Loop
Test to see if A, B, and C are numeric. E.G.
vb Code:
If Not IsNumeric(A) Then
'Do Something
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|