Homework Help!!!

tj107us1

In Runtime
Messages
332
Location
USA
Hey guys, I'm having trouble with my program, It suppose to take in a number as "totalSales" from the user and calculate the county and state tax and then calculate the total between the two. Then it displays all three answers( total, state, county) to console. But when I put in 4,545 as a number(totalSales) the county tax shows 4,545 instead of 90.9 that is suppose to be. I can't figure out whats going on!! The state tax works like it supposed to, but county tax don't and their the same besides the variable name. Oh by the way this is written in VB!!

thanks for any help you may have!!!
tj


p.s. I have to turn it in by Sunday night!!


works better like this:


Module Module1

Sub Main()

Dim totalSales As Double
Dim countyTax As Double
Dim stateTax As Double
Dim totalTax As Double


inputSales(totalSales)
calcCounty(totalSales, countyTax)
calcState(totalSales, stateTax)
calcTotal(countyTax, stateTax, totalTax)
printTax(totalSales, countyTax, stateTax)



Console.ReadLine()


End Sub


'FUNCTIONS


'inputData Function

Sub inputSales(ByRef totalSales)
Console.WriteLine("Enter the total sales for the month")
totalSales = Console.ReadLine()
End Sub



'calcCounty Function

Sub calcCounty(totalSales, ByRef countyTax)
countyTax = totalSales * 0.02
End Sub

'calcState Function

Sub calcState(totalSales, ByRef stateTax)
stateTax = totalSales * 0.04
End Sub


'totalTax Fucntion

Sub calcTotal(ByRef totalTax, stateTax, countyTax)
totalTax = countyTax + stateTax
End Sub


'PrintTax Function

Sub printTax(countyTax, stateTax, totalTax)
Console.WriteLine("The county tax is... " & countyTax)
Console.WriteLine("The state tax is... " & stateTax)
Console.WriteLine("The total tax for both State and County is... " & totalTax)
End Sub



End Module
 

Attachments

  • TaxCal.txt
    1.3 KB · Views: 2
Last edited:
Simple fix actually. Take a look a the code below. You pass in code to PrintTax as total sales, county tax, state tax but the function accepts it in the order of county tax, state tax and total tax. That's why it's coming out weird.

Code:
printTax(totalSales, countyTax, stateTax)
        Sub printTax(countyTax, stateTax, totalTax)
 
Thanks for the help! I changed the order of the arguments, but it still isn't working. Both county tax and state tax come out the same amount, and the total tax is the same amount as what I put in as input!!!. here is the modified code:

Module Module1

Sub Main()

Dim totalSales As Double
Dim countyTax As Double
Dim stateTax As Double
Dim totalTax As Double


inputSales(totalSales)
calcCounty(totalSales, countyTax)
calcState(totalSales, stateTax)
calcTotal(countyTax, stateTax, totalTax)
printTax(totalSales, countyTax, stateTax)



Console.ReadLine()


End Sub


'FUNCTIONS


'inputData Function

Sub inputSales(ByRef totalSales)
Console.WriteLine("Enter the total sales for the month")
totalSales = Console.ReadLine()
End Sub



'calcCounty Function

Sub calcCounty(totalSales, ByRef countyTax)
countyTax = totalSales * 0.02
End Sub

'calcState Function

Sub calcState(totalSales, ByRef stateTax)
stateTax = totalSales * 0.04
End Sub


'totalTax Fucntion

Sub calcTotal(ByRef totalTax, stateTax, countyTax)
totalTax = countyTax + stateTax
End Sub


'PrintTax Function

Sub printTax(totalTax, countyTax, stateTax)
Console.WriteLine("The county tax is... " & countyTax)
Console.WriteLine("The state tax is... " & stateTax)
Console.WriteLine("The total tax for both State and County is... " & totalTax)
End Sub



End Module
 
Just landed and haven't looked at the second set, but what was wrong?

Sent from my SCH-I545 using Computer Forums mobile app
 
Curious as to why you are doing everything by reference instead of just returning values from your functions...

for example inputSales should be:
Code:
Function inputSales() As Double
     Console.WriteLine("Enter the total sales for the month")
     Double totalSales = Console.ReadLine()
     return totalSales;
End Function

I guess if this is how your teacher is showing you, stick to it so you get a better grade, but generally if you only have one value to return it should be a function (even if you have more than one, it should be a function with a custom class to return).

Adam
 
Just landed and haven't looked at the second set, but what was wrong?

Sent from my SCH-I545 using Computer Forums mobile app

I suspect something to do with this:
(what was said earlier)
Code:
calcTotal(countyTax, stateTax, totalTax)
....
Sub calcTotal(ByRef totalTax, stateTax, countyTax)
totalTax = countyTax + stateTax
End Sub
and
Code:
printTax(totalSales, countyTax, stateTax)

Sub printTax(totalTax, countyTax, stateTax)
Console.WriteLine("The county tax is... " & countyTax)
Console.WriteLine("The state tax is... " & stateTax)
Console.WriteLine("The total tax for both State and County is... " & totalTax)
End Sub

the order that you pass IS relevant for a start

Passing parameters to a function in order A=1 B=2 C=3
but reciving them as CBA means that C=1 B=2 and A=3, because you pass reference to the memory location where they are stored, not the name of the variable.

so in the first instance the code was putting out funny data because variables were passed
Code:
printTax(totalSales, countyTax, stateTax)
Sub printTax(countyTax, stateTax, totalTax)

-so where he expected to show county tax, total sales is showing, where he expected to show state tax the county tax is showing and where he expected to show the total tax the state tax is showing.

In the second code block only half of the first problems are resolved.
Code:
printTax(totalSales, countyTax, stateTax)
Sub printTax(totalTax, countyTax, stateTax)
total tax is still filled with the data of total sales, though now state and county tax should be showing successfully.

the reason that County and state tax were showing as the same is this bit of code.

Code:
calcTotal(countyTax, stateTax, totalTax)
....
Sub calcTotal(ByRef totalTax, stateTax, countyTax)
totalTax = countyTax + stateTax
End Sub

again, you're passing and initialising values in weird weird ways

and should find that chaning that to

Code:
calcTotal(countyTax, stateTax, totalTax)
....
Sub calcTotal(countyTax, stateTax, ByRef totalTax)
totalTax = countyTax + stateTax
End Sub

works...

so to sum up.

change Line 11
Code:
printTax(totalSales, countyTax, stateTax)
to
Code:
printTax(totalTax, countyTax, stateTax)

and change Line 29
Code:
Sub calcTotal(ByRef totalTax, stateTax, countyTax)
to
Code:
Sub calcTotal(countyTax, stateTax, ByRef totalTax)
and it should work...



lessons learned pay attention to what you call your variables and how you order them when you pas functions!

(it would be nice if you posted your entire working code so that others could see the complete project all working.)
 
Back
Top Bottom