0

I've got this program to calculate the cost of sheets of paper. First the prices and bundles are declared and intialized. Then I use if else statements with division and modulos to break down the bundle into charging rates. Although my paper bundle amounts are correctly computed, my total costs are off. Can anyone spot what I'm doing wrong here?

Module Paper
Sub Main()

    'declare variables
    Dim PaperAmountReq As Double
    Dim PricePer_1_Sheet As Double = 0.1
    Dim PricePer_100_Sheets As Double = 0.055
    Dim PricePer_500_Sheets As Double = 0.04
    Dim PricePer_1000_Sheets As Double = 0.03
    Dim NumberOfSingles As Double = 0
    Dim NumberOf100s As Double = 0
    Dim NumberOf500s As Double = 0
    Dim Numberof1000s As Double = 0

    Dim TotalCost As Double


    Console.Write("Enter number of sheets of paper needed: ")
    PaperAmountReq = Console.Readline


    If PaperAmountReq / 1000 >= 1 Then
        Numberof1000s = PaperAmountReq \ 1000
        PaperAmountReq = PaperAmountReq Mod 1000

    End If

    If PaperAmountReq / 500 >= 1 Then
        NumberOf500s = PaperAmountReq \ 500
        PaperAmountReq = PaperAmountReq Mod 500

    End If

    If PaperAmountReq / 100 >= 1 Then
        Numberof100s = PaperAmountReq \ 100
        PaperAmountReq = PaperAmountReq Mod 100

    End If

    If PaperAmountReq / 1 = PaperAmountReq Then
        NumberOfSingles = PaperAmountReq

    End If

    'TotalCost = (Convert.ToDouble(Numberof1000s) * PricePer_1000_Sheets) + (Convert.ToDouble(NumberOf500s) * PricePer_500_Sheets) + (Convert.ToDouble(Numberof100s) * PricePer_100_Sheets) + (Convert.ToDouble(NumberOfSingles) * PricePer_1_Sheet)
    TotalCost = (Numberof1000s * PricePer_1000_Sheets) + (NumberOf500s * PricePer_500_Sheets) + (Numberof100s * PricePer_100_Sheets) + (NumberOfSingles * PricePer_1_Sheet)


    Console.WriteLine("Number of 1000 packages: " & Numberof1000s)
    Console.WriteLine("Number of 500 packages: " & Numberof500s)
    Console.WriteLine("Number of 100 packages: " & Numberof100s)
    Console.WriteLine("Number of singles packages: " & NumberOfSingles)
    Console.Write("Total Cost: " & TotalCost)

End Sub

End Module

user465001
  • 806
  • 13
  • 29
  • The issue might be lack of precision in doubles... http://stackoverflow.com/q/1165761/1316573 In what way are your results off? – Daniel Sep 27 '12 at 17:07

4 Answers4

2

With this:

If PaperAmountReq / 1000 >= 1 Then
    Numberof1000s = PaperAmountReq \ 1000
    PaperAmountReq = PaperAmountReq Mod 1000

End If

if you have 1500, you are setting Numberof1000s equal to 1 - not 1000. So when you calculate your total cost, your value of (presumably) cost/sheet is being multiplied by 1 instead of 1000.

Either change your equation here

TotalCost = (Numberof1000s * PricePer_1000_Sheets) + (NumberOf500s * PricePer_500_Sheets) + (Numberof100s * PricePer_100_Sheets) + (NumberOfSingles * PricePer_1_Sheet)

to be something like

TotalCost = (Numberof1000s * PricePer_1000_Sheets * 1000) + (NumberOf500s * PricePer_500_Sheets * 500) + (Numberof100s * PricePer_100_Sheets * 100) + (NumberOfSingles * PricePer_1_Sheet * 1)

or adjust the PricePer_XXX_Sheets values to be the equivalent values.

Dim PricePer_1_Sheet As Double = 0.1
Dim PricePer_100_Sheets As Double = 0.055 * 100
Dim PricePer_500_Sheets As Double = 0.04 * 500
Dim PricePer_1000_Sheets As Double = 0.03 * 1000
enderland
  • 13,825
  • 17
  • 98
  • 152
  • In this visual basic book I have, it says that the backward slash is for integer division. It truncates the remainder - the ".5". Wouldn't that make `1500\1000 = 1`. Doesn't that mean that the remainder is dropped completely? – user465001 Sep 27 '12 at 17:01
  • @user465001 yes, I mis-read that. However that only reinforces my confidence the second part of my answer is your issue. – enderland Sep 27 '12 at 17:05
  • It's working now due to the second part of your answer. Thanks for your help enderland. – user465001 Sep 27 '12 at 17:12
2

Well your numberof variables should be integers or maybe bigintegers if not big enough as should PaperAmountReq

Your code seems to be assuming integer division in these segments

If PaperAmountReq / 500 >= 1 Then          
  NumberOf500s = PaperAmountReq \ 500          
  PaperAmountReq = PaperAmountReq Mod 500 
End if   

in actual fact because you are using doubles, it's doing floating point division.

I'd also recommend that you use decimal instead of double for your price? and Total Cost variables.

Doubles are very precise, but often very inaccurate.

Tony Hopkinson
  • 20,172
  • 3
  • 31
  • 39
0

Looks like you need to convert the NumberOfXXXs variables to sheets, rather than bundles.

Your formula at the end uses per-sheet costs with per-bundle numbers. Either update the costs to be per-bundle, or update the bundle numbers to equal the round number of sheets in that bundle:

TotalCost = NumberOf1000s * 1000 * PricePer_1000_Sheets
Jake Bathman
  • 1,268
  • 3
  • 18
  • 25
0
Dim PricePer_100_Sheets As Double = 0.055 
Dim PricePer_500_Sheets As Double = 0.04 
Dim PricePer_1000_Sheets As Double = 0.03 

should be

Dim PricePer_100_Sheets As Double = 0.055 * 100
Dim PricePer_500_Sheets As Double = 0.04 * 500
Dim PricePer_1000_Sheets As Double = 0.03 * 1000
dfrevert
  • 366
  • 5
  • 17