'How do I debug this crash

My code:

Imports System

Module Program
    Sub Main(args As String())
        Dim Price As Integer
        Dim Qty As Integer
        Dim Total As Integer
        Console.Write("Enter the product unit price. ")
        Price = Console.ReadLine()
        Console.Write("Enter the product quantity. ")
        Qty = Console.ReadLine()
        Total = Price * Qty
        If (Qty > 99) And (Qty < 150) Then
            Console.WriteLine("You did not qualify for a discount. Your total is: " + Total.ToString)
        End If
        If (100 < Qty > 149) Then
            Console.WriteLine("You qualified for a 10% discount! Your total is: " + (Total * 1.1).ToString)
        End If
        If (Qty > 149) And (Qty < 200) Then
            Console.WriteLine("You qualified for a 15% discount! Your total is: " + (Total * 1.15).ToString)
        End If
        If (Qty > 199) Then
            Console.WriteLine("You qualified for a 20% discount! Your total is: " + (Total * 1.2).ToString)
        End If
    End Sub
End Module

I can run it to input the values but crash when the math component is executed. Any ideas on how to fix this silly little program?



Solution 1:[1]

This isn't a thing:

If (100 < Qty > 149) Then

If needs to look more like this:

If 100 <= Qty AndAlso Qty < 150 Then

The first condition should also just be If Qty < 100 Then

Worse, Qty is a STRING. For cultural/internationalization reasons, converting between strings and numbers is one of the slowest and most error-prone operations you can do within a single computer. It's something to minimize. Therefore you should parse the Qty and Price variables into actual Integer and Decimal variables once right after you read them from the user. Failure to do so makes the code seem amateurish.

Finally, rather than a bunch of If/Else conditions I'd put the data in an ordered map and take the first match:

Imports System
Imports System.Linq
Imports System.ValueTuple ' No longer needed on recent versions of VS

Module Program
    Sub Main(args As String())
        Console.Write("Enter the product unit price. ")
        Dim Price As Decimal = Decimal.Parse(Console.ReadLine())
        Console.Write("Enter the product quantity. ")
        Dim Qty As Integer = Integer.Parse(Console.ReadLine())

        Dim Total As Decimal = Price * Qty
        Dim discount As Decimal = GetDiscount(Qty)

        Dim discountMessage As String = "You did not qualify for a discount."
        If discount > 0.0D Then
           discountMessage = $"You qualified for a {discount:P0} discount!"
           Total *= (1.0D - discount)
        End If

        Console.WriteLine($"{discountMessage} Your total is: {Total:C2}")
    End Sub

    Public Function GetDiscount(Qty As Integer) As Decimal
         Dim discountMap() As (Integer, Decimal) = {
            (200, 0.20D),
            (150, 0.15D),
            (100, 0.10D),
            (Integer.MinValue, 0.00D)
        }
        Return discountMap.First(Function(m) m.Item1 <= Qty).Item2
    End Function
End Module

If some of this code looks strange to you, it relies on Tuples, a lambda expression, and string interpolation; all three techniques are good tricks to add to your programming repertoire.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1