-
Oct 12th, 2012, 01:31 PM
#1
Thread Starter
New Member
Having trouble with If Then command:
Hello to all, I have completed a class assignment and wanted to expand using an If Then module to determine if an input is either a number or a specific letter. I've been searching forums and have found several examples that I have tried to make work but am unable to succeed as of yet. The VB project is in console mode and I will attempt to cut and paste the code so you can take a look at it. Now, here's a discription of what my program is supposed to do. My program asks for numerical values to input to calculate the area of a triangle. I want to add an if then statement in the event that the input is not a number it will re-ask the user to input the number again. Here is what I have so far.
Sub Main()
Dim Number As Integer
Dim Display As String
Console.WriteLine("Please input a number")
Dim input = Console.ReadLine()
If input(Number) = True Then
Number = Val(Number)
If Number < 1 Or Number > 100 Then
If Number > 100 Then
Console.WriteLine(" The Number You entered is too high")
ElseIf Number < 1 Then
Console.WriteLine(" The Number You entered is too low")
ElseIf Number >= 1 And Number <= 100 Then
Process1.Enabled = False
End If
Else
Display = " The number you entered is not between 1 and 100"
End If
Else
Display = " You did not enter a numerical value "
End If
End Sub
How can I cut and paste what I have on VB to be the same in this forum. I used WYSIWYG and it still does not look like what I have on VB.
Thanks
-
Oct 12th, 2012, 01:51 PM
#2
Re: Having trouble with If Then command:
put [CODE][/CODE] or [HIGHLIGHT=VB][/HIGHLIGHT] tags around the code...
for starters though.... these two lines shouldn't work....
Code:
If input(Number) = True Then
Number = Val(Number)
The first should produce a compile error...
Input is going to be a string because that's what Console.ReadLine() returns.
Number is an integer, but hasn't been assigned a value yet so it's not really valid...
Then you have Number = Val(Number) ... but Number is still empty, and even if it did, it would already be an integer (because that's what Number has been defined as) ... making Number = Val(Number) redundant.
I'm guessing that's not really what you wanted....
Try this instead:
Code:
If Integer.TryParse(Input, Number) Then
If Number < 1 Or Number > 100 Then
If Number > 100 Then
This condition will never run:
Code:
ElseIf Number >= 1 And Number <= 100 Then
It is found inside the first check for out of bounds...
here's a little quick restructuing:
Code:
If Integer.TryParse(Input, Number) Then
If Number < 1 then
console.writeline("Value too small")
Display = "Value entered is not between 1 and 100"
elseif Number > 100 then
console.writeline("Value too large")
Display = "Value entered is not between 1 and 100"
else ' No need for any further checking we know it's between 1 and 100
Process1.Enabled = False
End If
Else
Display = " You did not enter a numerical value "
End If
-tg
-
Oct 12th, 2012, 03:13 PM
#3
Thread Starter
New Member
Re: Having trouble with If Then command:
I will give it a try, Thank You...so many questions, so little time in the day!
-
Oct 12th, 2012, 07:23 PM
#4
Thread Starter
New Member
Re: Having trouble with If Then command:
Ok I've been working on this now for several hours and am still having some issues. Maybe I just need to simplify this by breaking it down into two steps. Here is what I now have.
VB Code:
Module Module1 Sub Main() Console.WriteLine("please enter a number between 1 and 9, or one of the following letters a, b or c") 'here is where I would like the program to determine if the input is a number or a letter 'unfortunatly my text book in on program structure and not on VB so no useable code is 'in the book. Dim input = Console.ReadLine() If input <= 1 OrElse >= 9 Then Else input <> (a,b,c) then Console.WriteLine("Im sorry, but you did not enter one of the designated characters. Please try again") End If End Sub End Module
-
Dec 14th, 2012, 04:56 AM
#5
Re: Having trouble with If Then command:
Does this do what you want?
Code:
Option Compare Binary
Option Explicit On
Option Infer Off
Option Strict On
Imports System
Public Module AreaModule
Public Sub Main ()
Dim Side1 As Integer = GetNumber("Length of side #1: ")
Dim Side2 As Integer = GetNumber("Length of side #2: ")
Dim Area As Integer = CInt((Side1 * Side2) / 2)
Console.Write ("The triangle's area: {0}", Area)
End Sub
Private Function GetNumber(Prompt As String) As Integer
Dim UserInput As String = Nothing
Do
Console.Write (Prompt)
UserInput = Console.ReadLine().Trim
Loop Until Integer.TryParse(UserInput, New Int32)
Return CInt(UserInput)
End Function
End Module
The program checks whether the user's input is a valid number using the TryParse() function.
Hope this helps.
Last edited by Peter Swinkels; Dec 2nd, 2015 at 05:24 AM.
Reason: Updated the code.
Tags for this Thread
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
|