Reputation: 15
I am attempting to create a calculator where a user inputs a number (1-20) and it gives the user the option to get the sum or product from 1 to the number they entered. ie the sum of 5 would be 15 while the product would be 120. I am using select case and a for next loop. I have managed to get the sum portion of the calculator working correctly and thought I could use the same principle for the product portion but I am having no such luck. Any help or a gentle nudge in the right direction would greatly appreciated. Cheers.
Code:
Dim intsum As Integer
Dim intnum As Integer
Dim intproduct As Integer
If IsNumeric(txtinput.Text) Then
intnum = CInt(txtinput.Text)
Else
MessageBox.Show("Please enter a numeric value", "Input error")
txtinput.Clear()
txtinput.Focus()
End If
If intnum >= 1 AndAlso intnum <= 20 Then
Select Case True
Case btnproduct.Checked
For P = 1 To intnum Step 1
intproduct = intproduct * P
Next
Case btnsum.Checked
For S = 1 To intnum Step 1
intsum = intsum + S
Next
End Select
Upvotes: 1
Views: 1765
Reputation: 1506
You don't need to check the value of intnum
if the value is not numeric.
Dim intsum As Integer
Dim intnum As Integer
Dim intproduct As Integer
If IsNumeric(txtinput.Text) Then
intnum = CInt(txtinput.Text)
If intnum >= 1 AndAlso intnum <= 20 Then
Select Case True
Case btnproduct.Checked
For P = 1 To intnum Step 1
intproduct = intproduct * P
Next
Case btnsum.Checked
For S = 1 To intnum Step 1
intsum = intsum + S
Next
End Select
End If
Else
MessageBox.Show("Please enter a numeric value", "Input error")
txtinput.Clear()
txtinput.Focus()
End If
Upvotes: 0
Reputation: 805
Initialise intproduct to = 1 as at the moment you are multiplying your value by 0 so it will always show the final result as 0.
Upvotes: 1