Discussion – Lesson 7 - Sanitizing user input in VB.NET
BackComments

Member

6 messages from 6 displayed.
//= Settings::TRACKING_CODE_B ?> //= Settings::TRACKING_CODE ?>
Comments
Thanks for pointing that out! There's been a typo in the code. There should
be Case "y"
instead of Case "a"
. Please fix that in
your code and it should work then
Still, there is a problem in that code, but if you change the values to "yes"
and "no" it's working fine.
Select Case Console.ReadKey().KeyChar.ToString().ToLower()
Case "y"
goOn = True
validChoice = True
Case "n"
goOn = False
validChoice = True
Working Code
Select Case Console.ReadKey().KeyChar.ToString().ToLower()
Case "y"
goOn = "yes"
validChoice = True
Case "n"
goOn = "no"
validChoice = True
Please have a look at the image.
Thank you
was
A modified version of your code, everything works perfectly. Thank you
Dim goOn As Boolean = True
Dim crlf As String = Chr(13) + Chr(10)
While goOn
'reading numbers
Console.WriteLine(crlf + "Welcome to our calculator!")
Console.WriteLine("==========================")
Console.WriteLine(crlf + "Enter the first number:")
Dim a As Double
While Not Double.TryParse(Console.ReadLine(), a)
Console.WriteLine(crlf + "Invalid entry, try again:")
End While
Console.WriteLine(crlf + "Enter the second number:")
Dim b As Double
While Not Double.TryParse(Console.ReadLine(), b)
Console.WriteLine(crlf + "Invalid entry, try again:")
End While
'operation choice and calculation
Console.WriteLine(crlf + "Choose one of the following operations:")
Console.WriteLine("1- Addition:")
Console.WriteLine("2- Subtraction:")
Console.WriteLine("3- Multiplication:")
Console.WriteLine("4- Division:")
Dim choice As Char = Console.ReadKey().KeyChar
Dim result As Double = 0
Dim validChoice As Boolean = True
Select Case choice
Case "1"
result = a + b
Case "2"
result = a - b
Case "3"
result = a * b
Case "4"
result = a / b
Case Else
validChoice = False
End Select
If validChoice Then
Console.WriteLine(crlf + "Result: {0}", result)
Else
Console.WriteLine("Invalid choice")
End If
Console.WriteLine(crlf + "Would you like to make another calculation? [yes/no]")
'request to continue
validChoice = False
While Not validChoice
Select Case Console.ReadKey().KeyChar.ToString().ToLower()
Case "y"
goOn = True
validChoice = True
Case "n"
goOn = False
validChoice = True
Case Else
Console.WriteLine(crlf + "Invalid option, please enter y/n")
End Select
End While
End While
Console.WriteLine("Thank you for using our calculator. Press any key to end the program.")
Console.ReadKey()
6 messages from 6 displayed.