Get up to 80 % extra points for free! More info:

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

Back

 

Comments
Avatar
Fabrice Bouka
Member
Avatar
Fabrice Bouka:2/27/2018 21:51

Hi,
I've tried to make some changes in the last code of the lesson 7. Please, could check it? Thanks!

Module Module1

    Sub Main()

        Dim goOn As String = "yes"
        While goOn = "yes"
            ' reading numbers
            Console.WriteLine("Enter the first number:")
            Dim a As Double
            While Not Double.TryParse(Console.ReadLine(), a)
                Console.WriteLine("Invalid entry, please try again:")
            End While
            Console.WriteLine("Enter the second number:")
            Dim b As Double
            While Not Double.TryParse(Console.ReadLine(), b)
                Console.WriteLine("Invalid entry, please try again:")
            End While
            ' operation choice and calculation
            Console.WriteLine("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
            Console.WriteLine()

            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("Result: {0}", result)
            Else
                Console.WriteLine("Invalid choice")
            End If
            Console.WriteLine("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 = "yes"
                        validChoice = True
                        Console.WriteLine()
                    Case "n"
                        goOn = "no"
                        validChoice = True
                        Console.WriteLine()
                    Case Else

                        Console.WriteLine("Invalid option, please enter yes/no")
                End Select
            End While
        End While
        Console.WriteLine("Thank you for using our calculator. Press any key to end the program.")
        Console.ReadKey()
    End Sub
End Module
Edited 2/28/2018 5:06
 
Reply
2/27/2018 21:51
Avatar
Replies to Fabrice Bouka
David Capka Hartinger:2/28/2018 5:07

Seems fine to me, good job :)

Reply
2/28/2018 5:07
You can walk through a storm and feel the wind but you know you are not the wind.
Avatar
wasim
Member
Avatar
wasim:8/21/2019 23:39

Hi David, there is an error in your code, if you enter 'y' it's just continuing the loop with the same statement, please have a look at the attached image.
Cheers.

 
Reply
8/21/2019 23:39
Avatar
David Jancik
Owner
Avatar
Replies to wasim
David Jancik:8/22/2019 12:18

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 :)

Reply
8/22/2019 12:18
Forget that it's impossible and do it ;)
Avatar
wasim
Member
Avatar
wasim:8/29/2019 0:07

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.ReadKe­y().KeyChar.ToS­tring().ToLower()
Case "y"
goOn = True
validChoice = True
Case "n"
goOn = False
validChoice = True


Working Code

Select Case Console.ReadKe­y().KeyChar.ToS­tring().ToLower()
Case "y"
goOn = "yes"
validChoice = True
Case "n"
goOn = "no"
validChoice = True
Please have a look at the image.
Thank you
was

Edited 8/29/2019 0:09
 
Reply
8/29/2019 0:07
Avatar
wasim
Member
Avatar
wasim:8/29/2019 1:22

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()
Edited 8/29/2019 1:23
 
Reply
8/29/2019 1:22
To maintain the quality of discussion, we only allow registered members to comment. Sign in. If you're new, Sign up, it's free.

6 messages from 6 displayed.