Problem involving salesperson - vb.net

Okey my question is when you run this you do not get no dollar amount in the monthly catergory Can someone tell me what I am doing wrong.Cause if you type in allen for name and code 1 monthly sales should be 500 and commission should be 1005. I just can not figure out what I am doing wrong.
Option Strict On
Option Explicit On
Module Module1
Sub Main()
Dim salesPersonName As String
Dim monthlySales, SalesPersonCommission, salesmancode As Integer
Dim totalSales, totalCommission As Integer
Console.WriteLine("Please enter your name: ")
salesPersonName = Console.ReadLine()
Do While Not (salesPersonName.Equals("Eugene"))
Console.WriteLine("Please enter a sales code: ")
salesmancode = CInt(Console.ReadLine())
Select Case salesmancode
Case 1
SalesPersonCommission = CInt(0.01 * monthlySales + 1000)
Case 2
SalesPersonCommission = CInt(0.03 * monthlySales)
Case 3
SalesPersonCommission = CInt(0.04 * monthlySales + 500)
Case Is < 3
SalesPersonCommission = CInt(0.02 * monthlySales + 200)
End Select
totalCommission = monthlySales + totalSales
Console.WriteLine("Please enter your name= " & salesPersonName)
Console.WriteLine("Monthly Sales= " & monthlySales)
Console.WriteLine("Sales Code= " & salesmancode)
Console.WriteLine("Sales Person Commission= " & SalesPersonCommission)
Console.WriteLine("Please enter your name:")
salesPersonName = Console.ReadLine()
Loop
Console.WriteLine("Total Commission Dollars= " & monthlySales + totalSales)
End Sub
End Module

You're not assigning an amount to monthlySales before using it in the calculation.
Perhaps you mean to use those:
Select Case salesmanCode
CASE 1
monthlySales = 500
....
End Select
SalesPersonCommission = CInt(0.01 * monthlySales + 1000)
Depending on what exactly you're trying to do, you may want to surround part of your calculation in parentheses to indicate clearly what order you want the calculations to occur. The first calculation below gives a different result than the second:
SalesPersonCommission = CInt(0.01 * (monthlySales + 1000))
SalesPersonCommission = CInt((0.01 * monthlySales) + 1000)
The second is what you want.

It looks like you're forgetting to set monthlySales.
If you set it to 500 you get the results you say you're expecting.

Related

Converting cents into amount of change given

So i am having troubles with this code. I get the dollars part to work correctly but the rest is not. Im trying to change 529 cents into 5 dollars 1 quarter 0 dimes 0 nickels 4 pennies, here is my code.
Option Explicit On
Option Strict On
Module Module1
Sub Main()
Dim amount As Integer
Dim dollars As Integer
Dim quarters As Integer
Dim dimes As Integer
Dim nickels As Integer
Dim pennies As Integer
Console.WriteLine("Please Enter A Unit of Cents")
amount = CInt(Console.ReadLine())
If (amount >= 100) Then
dollars = amount \ 100
amount = amount - (100 * dollars)
ElseIf (amount >= 25) Then
quarters = amount \ quarters
amount = amount - (25 * quarters)
ElseIf (amount >= 10) Then
dimes = amount \ dimes
amount = amount - (10 * dimes)
ElseIf (amount >= 5) Then
nickels = amount \ nickels
amount = amount - (5 * nickels)
ElseIf (amount >= 1) Then
pennies = amount
End If
Console.WriteLine("" & dollars & " dollars " & quarters & " quarters " & dimes & " dimes " & nickels & " nickels " & pennies & " pennies ")
Console.ReadLine()
End Sub
End Module
Write a function to automate the counting of coins. You were almost there. Just use modulo to find the remaining pennies each time a count is done.
Sub Main()
Dim amount As Integer
Dim dollars As Integer
Dim quarters As Integer
Dim dimes As Integer
Dim nickels As Integer
Dim pennies As Integer
Console.WriteLine("Please Enter A Unit of Cents")
amount = CInt(Console.ReadLine())
dollars = parseDenomination(amount, 100)
quarters = parseDenomination(amount, 25)
dimes = parseDenomination(amount, 10)
nickels = parseDenomination(amount, 5)
pennies = amount
Console.WriteLine("" & dollars & " dollars " & quarters & " quarters " & dimes & " dimes " & nickels & " nickels " & pennies & " pennies ")
Console.WriteLine($"{getAmountString(dollars, "Dollar")}, {getAmountString(quarters, "Quarter")}, {getAmountString(dimes, "Dime")}, {getAmountString(nickels, "Nickel")}, {getAmountString(pennies, "Penny")},")
Console.ReadLine()
End Sub
Private Function parseDenomination(ByRef amount As Integer, count As Integer) As Integer
Dim result = amount \ count
amount = amount Mod count
Return result
End Function
Private Function getAmountString(amount As Integer, name As String) As String
Return $"{name}{If(amount = 1, "", "s")}: {amount}"
End Function
I added an additional method to help get the string output. It kind of breaks down for "pennies / pennys" though - that's English for you.

How to remove decimal from variable in vb.net

I have created a code to work out pay of workers, and to - 1 of pay for charity if it is over 100, and every 10 over 100 -1. But I need to get rid of the decimal with new pay variable. At the end of the if function
Dim donation As Single
Dim newpay As Single
Dim hourlyRate As Decimal
Dim pay As Decimal
Dim hours As Single
Sub Main()
Console.WriteLine("how many hours have you worked this week?")
hours = Console.ReadLine
Console.WriteLine("what is your hourly pay?")
hourlyRate = Console.ReadLine
pay = hours * hourlyRate
newpay = pay
If pay > 100 Then
pay = pay - 1
newpay = newpay - 100
donation = donation + 1
newpay = newpay / 10
Math.Floor(newpay)
pay = pay - newpay
donation = donation + newpay
End If
Console.WriteLine("you earned £" & pay & " and donated £" & donation & " to charity")
Console.ReadLine()
End Sub
Use the Math.Floor function to round down
SO if newpay was 3.6
Math.Floor(newpay)
will return 3
SO
Use the result of this function
donation = donation + Math.Floor(newpay)
OR
newpay = Math.Floor(newpay)

Input validation and repetition in vba

I have the following code and I would like help in 4 areas:
Did I validate the inputbox correctly? It should only take positive numerical variables.
How can I let the input box accept both input with, and without, symbols such as ($)?
How can I link the code so that it can directly request another number if the user has entered a negative non numeric number?
How can I repeat the procedure in the loop directly without repeating the whole code?
Option Explicit
Sub IncomeSalaryCalculation()
Dim strSalary As String
Dim dblTaxableSalary As Double
Dim dblTax As Double
Dim dblSalaryAfterTax As Double
Dim strOutput As String
Dim SalaryCalculationRequest As VbMsgBoxResult
strSalary = InputBox("Please indicate your salary", "Salary Calculation")
If Not IsNumeric(strSalary) Then
MsgBox "This is no number! Please enter a non-negatif number", vbOKOnly, "Error"
ElseIf strSalary < 0 Then
MsgBox "You should enter a positive number", vbOKOnly, "Error"
Else
dblTaxableSalary = CDbl(strSalary)
Select Case dblTaxableSalary
Case Is >= 151000
dblTax = 2440 * 0.1 + (37400 - 2440) * 0.2 + (150000 - 37400) * 0.5 + (dblTaxableSalary - 150000) * 0.5
Case Is >= 37401
dblTax = 2440 * 0.1 + (37400 - 2440) * 0.2 + (dblTaxableSalary - 37400) * 0.4
Case Is >= 2441
dblTax = 2440 * 0.1 + (dblTaxableSalary - 2440) * 0.2
Case Else
dblTax = 2440 * 0.1
End Select
dblSalaryAfterTax = dblTaxableSalary - dblTax
strOutput = "The amount of income tax is " & dblTax & vbNewLine & "Your salary after tax is " & dblSalaryAfterTax
MsgBox strOutput, vbOKOnly, "Final Salary"
Do
SalaryCalculationRequest = MsgBox("Do you want to calculate the tax of a new income?", vbYesNo, "New income tax salary calculation")
If SalaryCalculationRequest = vbYes Then
strSalary = InputBox("Please indicate your salary", "Salary Calculation")
dblTaxableSalary = CDbl(strSalary)
Select Case dblTaxableSalary
Case Is >= 151000
dblTax = 2440 * 0.1 + (37400 - 2440) * 0.2 + (150000 - 37400) * 0.5 + (dblTaxableSalary - 150000) * 0.5
Case Is >= 37401
dblTax = 2440 * 0.1 + (37400 - 2440) * 0.2 + (dblTaxableSalary - 37400) * 0.4
Case Is >= 2441
dblTax = 2440 * 0.1 + (dblTaxableSalary - 2440) * 0.2
Case Else
dblTax = 2440 * 0.1
End Select
dblSalaryAfterTax = dblTaxableSalary - dblTax
strOutput = "The amount of income tax is " & dblTax & vbNewLine & "Your salary after tax is " & dblSalaryAfterTax
MsgBox strOutput, vbOKOnly, "Final Salary"
Else
MsgBox "Glad to serve you"
End If
Loop Until SalaryCalculationRequest = vbNo
End If
End Sub
This should answer your questions without delving into all your code. Tested.
Sub getInput()
Dim ans As String
Do
ans = InputBox("Please indicate your salary", "Salary Calculation")
Loop Until isValid(ans)
''{{{{{{{{
''Do what you need here with the user input (stored in *ans* variable)
''}}}}}}}}
End Sub
Function isValid(ans As String) As Boolean
Dim cleanAns As variant
cleanAns = IIf(ans Like "*$*", Replace(ans, "$", ""), ans)
isValid = IsNumeric(cleanAns) And cleanAns > 0
If Not isValid Then MsgBox "only positive numbers allowed"
End Function

Receive "infinity" as result in mortgage payment calculation in Visual Basic

I'm a first year programming student and am trying to make a mortgage payment calculator. Users enter their loan amount and interest rate, select 15 or 30 years for the term length, and their monthly payment should be calculated. I think the math is correct, but I think I'm running into problems with converting data types. The only result I'm able to calculate is "Infinity". I've also tried declaring all variables as Doubles, but no difference.
Below is the piece of code I'm having trouble with, and Option Strict On is included in the full code. Any tips would be greatly appreciated!
Dim decLoanAmount As Decimal
Dim decInterestRate As Decimal
Dim decFifteen As Decimal = 180D
Dim decThirty As Decimal = 360D
Dim decNumberOfMonths As Decimal
Dim dblPayment As Double
Dim decPayment As Decimal
' Did user enter a numeric value?
If IsNumeric(txtLoanAmount.Text) And IsNumeric(txtInterestRate.Text) Then
decLoanAmount = Convert.ToDecimal(txtLoanAmount.Text)
decInterestRate = Convert.ToDecimal(txtInterestRate.Text)
' Is Loan Amount greater than zero?
If decLoanAmount > 0 And decInterestRate > 0 Then
If radFifteen.Checked = True Then
decNumberOfMonths = decFifteen
ElseIf radThirty.Checked = True Then
decNumberOfMonths = decThirty
End If
' Calculate the monthly payments as a double
dblPayment = (decLoanAmount * (decInterestRate / 12 / 100) * (1 + (decInterestRate / 12 / 100) _
^ decNumberOfMonths)) / ((1 + (decInterestRate / 12 / 100) ^ decNumberOfMonths) - 1)
' Convert double to decimal
decPayment = Convert.ToDecimal(decPayment)
' Display monthly payment
lblPayment.Text = decPayment.ToString("C2")
Else
If decLoanAmount < 0 Then
MsgBox("Please enter a valid loan amount.", , "Input error")
End If
If decInterestRate < 0 Then
MsgBox("Please enter a valid interest rate.", , "Input error")
End If
End If
Else
' Display error message if user entered a negative value.
MsgBox("Please enter a numeric value.", , "Input Error")
End If
End Sub
Based on the Wikipedia formula, you aren't doing it correctly. It should look something like this:
dblPayment = (decLoanAmount * (decInterestRate / (12 * 100))) / (1 - (1 + (decInterestRate / (12 * 100))) ^ (-decNumberOfMonths))
Also, this line is weird
decPayment = Convert.ToDecimal(decPayment)
Should be
decPayment = Convert.ToDecimal(dblPayment)

I cant get the loop right

Ok I have got this far and if you run it, it will do what you ask. Now when I type in 99999 or -99999 it will not end. Can someone tell me what I am doing wrong. I am suppose to loop until a sentinel value of -99999 is enter for previous meter reading.
Sub Main()
' program to compute a consumer’s electric bill. It will calculate the bill for one or more customers
' by looping until a sentinel value of -99999 is entered for the previous meter reading.
Dim previousReading As Integer = 0
Dim currentReading As Integer = 0
Do While (previousReading <> -99999)
Dim salesTax As Double
' prompt user to input value for previous reading then convert to integer
Console.WriteLine("Enter the value of previous meter reading")
previousReading = Convert.ToInt32(Console.ReadLine())
' prompt user to input value for current reading then convert to integer
Console.WriteLine("Enter the value of current meter reading")
currentReading = Convert.ToInt32(Console.ReadLine())
Dim kwhConsumed As Integer
Dim electricCharge, totalBill As Double
' calculate KWH consumed
kwhConsumed = currentReading - previousReading
' Use select case to determine electricCharge
Select Case kwhConsumed
Case Is < 500
electricCharge = kwhConsumed * 0.05
Case 500 To 1000
electricCharge = 25 + ((kwhConsumed - 500) * 0.055)
Case Is > 1000
electricCharge = 52.5 + ((kwhConsumed - 1000) * 0.06)
End Select
' calculate sales tax
salesTax = electricCharge * 0.085
' calculate total charges
totalBill = electricCharge + salesTax
' Output values for kwhConsumed, electricCharge, salesTax, and totalBill
Console.WriteLine("KWH consumed = " & kwhConsumed & " KWH")
Console.WriteLine("Electric charge = $" & Math.Round(electricCharge, 2))
Console.WriteLine("Sales tax = $" & Math.Round(salesTax, 2))
Console.WriteLine("Total bill = $" & Math.Round(totalBill, 2))
Loop
End Sub
You can try using string comparison instead for previousReading <> -99999. You also need to use absolute value to consider both -99999 and 99999. Do something like this
Do While (previousReading <> 99999)
//code
previousReading = Math.Abs(Convert.ToInt32(Console.ReadLine()))
//code
Loop
I'm guessing this is homework?
Instead of blurting out the answer, I wonder if you might think about inserting a Debug.Print statement and some kind of "break" statement after your previousReading = Convert.ToInt32 statement. To look for the "break" statement, search for "vb.net exit loop" and see what pops up.