Jeremy Flaugher
Jeremy Flaugher

Reputation: 437

VB Gross Pay Calculations

All I am trying to do is create a simple pay calculator. The user types in their baseSalary, their totalSales, and their commissionRate. The program runs just fine, but the totalPay isn't comming out right. Any help would be awesome. This is most likely a simple fix, but as this is only my second day of my VB class I am clueless as how to fix this. Thank you in advance for any help given!

'   Total Pay Calculator
'   By: Jeremy Flaugher
'   01/17/2013


Module myPay
    Sub Main()
        '   Declare the variables
        Dim baseSalary As Integer
        Dim totalSales As Integer
        Dim commissionRate As Decimal
        Dim totalPay As Integer

        '   Title and By line
        Console.Out.WriteLine("Welcome to the Paycheck Calculator")
        Console.Out.WriteLine("Created by Jeremy Flaugher" & vbCrLf)

        '   User Prompts
        Console.Out.Write("Please enter your Base Salary: $")
        baseSalary = Console.In.ReadLine()

        Console.Out.Write("Please enter your number of sales: ")
        totalSales = Console.In.ReadLine()

        Console.Out.Write("Please enter your Commission Rate in decimal form: ")
        commissionRate = Console.In.ReadLine()

        '   Processes
        totalPay = (totalSales * commissionRate) + baseSalary
        Console.Out.WriteLine("Your paycheck will total: " & (FormatCurrency(totalPay)))
        Console.ReadKey()
    End Sub
End Module

I am checking the output on a calculator after running the program. Say I enter $100 as the base pay, 5 as the number of sales, and .5 as the commission rate. On the calculator I am getting $102.5 as the total pay, but the when running the program I am getting $102.00. How can I fix this.

Upvotes: 0

Views: 3316

Answers (1)

Mark Hall
Mark Hall

Reputation: 54532

First of all I would put Option Strict On at the top of your module, it is good practice and will save you grief trying to figure out why things are not correct. In your case I believe your problem is that you are using a decimal as part of your calculation but you are putting the result into an Integer. Try declaring totalPay as a Decimal also.

The benefits of using Option Strict from above link:

When you set Option Strict to On, Visual Basic checks that data types are specified for all programming elements. Data types can be specified explicitly, or specified by using local type inference. Specifying data types for all your programming elements is recommended, for the following reasons:

  • It enables IntelliSense support for your variables and parameters. This enables you to see their properties and other members as you type code.
  • • It enables the compiler to perform type checking. Type checking helps you find statements that can fail at run time because of type conversion errors. It also identifies calls to methods on objects that do not support those methods.
  • • It speeds up the execution of code. One reason for this is that if you do not specify a data type for a programming element, the Visual Basic compiler assigns it the Object type. Compiled code might have to convert back and forth between Object and other data types, which reduces performance.

Upvotes: 2

Related Questions