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.
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:
This enables you to see their properties and other members as you
type code.
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.
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.