4 Replies - 484 Views - Last Post: 21 September 2010 - 08:57 AM Rate Topic: -----

#1 lmill616  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-September 10

code is not calculating right

Posted 21 September 2010 - 07:27 AM

Can someone help me figure out why my calculations do not come out right? I have many notes so it is easy to understand. The assignment is: "Create a windows app for the rental of a vehicle. User selects number of rental days up to 7 days. The user also
'can select one of three types of vehicles. Customer has a choice of filling the gas tank themselves,
'or prepaying for a full tank of gas ($52). If vehicle will be driven by more than one driver, a multiple
'driver cost of $22 per day will be added to the cost."

Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click


        Dim wranglerCost As Single = 55
        Dim cherokeeCost As Single = 85
        Dim roverCost As Single = 125
        Dim sngDays As Single
        Dim totalCost As Single
        Dim totalmultdrivers As Single

        'calculation for multiple driver option ( 22 multipled by number of days)
        totalmultdrivers = sngDays * 22


        If IsNumeric(txtDays.Text) Then
            sngDays = Convert.ToSingle(txtDays.Text)
            If sngDays >= 1 And sngDays <= 7 Then
                If radWrangler.Checked Then

                    'if user chooses no multiple drivers and no prepaid gas
                    'calculates right
                    If radNodrivers.Checked And radNoGas.Checked Then
                        totalCost = wranglerCost * sngDays


                        'if user chooses no mult drivers and yes to prepaid gas
                        'does not calc right
                    ElseIf radNodrivers.Checked And radYesGas.Checked Then
                        totalCost = wranglerCost * sngDays + 52


                        'if user chooses yes to mult drivers and yes to prepaid gas
                        'does not calc right
                    ElseIf radYesdrivers.Checked And radYesGas.Checked Then
                        totalCost = wranglerCost + totalmultdrivers * sngDays + 52


                        'if user chooses yes to mult drivers and no to prepaid gas
                        'does not calc right
                    ElseIf radYesdrivers.Checked And radNoGas.Checked Then
                        totalCost = wranglerCost + totalmultdrivers * sngDays
                    End If



Is This A Good Question/Topic? 0
  • +

Replies To: code is not calculating right

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: code is not calculating right

Posted 21 September 2010 - 07:36 AM

What is the error ? give explaination to your code
Was This Post Helpful? 0
  • +
  • -

#3 lmill616  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-September 10

Re: code is not calculating right

Posted 21 September 2010 - 07:49 AM

View Postsmohd, on 21 September 2010 - 06:36 AM, said:

What is the error ? give explaination to your code


My b. Ok so in the first If statement, the calculations are right, which shows the total cost of the rental if the user does not select prepaid gas, and does not select multiple drivers. For example, the wrangler costs $55 per day and if the first if statement is true, then the label will display $55 if the user types in 1 day. The second if statement also calculates right. But the third, and forth If statements do not work right. For example, user selects the wrangler ($55 per day, for one day) and also selects Yes to having prepaid gas and Yes to having multiple drivers ($22 extra per day). But instead of displaying the proper total of $129, it only displays $107 (which is the correct calculation for the second if statements). So basically it's not adding in the multiple driver fee of $22 per day and I do not know why.

my multiple driver calculation is displayed as:
totalmultdrivers = 22 * sngDays 



 'if user chooses no multiple drivers and no prepaid gas
                    'calculates right
                    If radNodrivers.Checked And radNoGas.Checked Then
                        totalCost = wranglerCost * sngDays


                        'if user chooses no mult drivers and yes to prepaid gas
                        'does calc right
                    ElseIf radNodrivers.Checked And radYesGas.Checked Then
                        totalCost = wranglerCost * sngDays + 52


                        'if user chooses yes to mult drivers and yes to prepaid gas
                        'does not calc right
                    ElseIf radYesdrivers.Checked And radYesGas.Checked Then
                        totalCost = wranglerCost + totalmultdrivers * sngDays + 52


                        'if user chooses yes to mult drivers and no to prepaid gas
                        'does not calc right



Was This Post Helpful? 0
  • +
  • -

#4 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1817
  • View blog
  • Posts: 4,625
  • Joined: 14-March 10

Re: code is not calculating right

Posted 21 September 2010 - 08:26 AM

Ok the error is that you are required to use nested if instead of if..elseif

the correction is:
 'all following if will be in the first if then
                    If radNodrivers.Checked And radNoGas.Checked Then
                        totalCost = wranglerCost * sngDays
                   'put here
                   endif

                        'use if and change to
                    If radNodrivers.Checked And radYesGas.Checked Then
                        totalCost = totalCost + wranglerCost * sngDays + 52
                        endif

                        'use if and change to
                    If radYesdrivers.Checked And radYesGas.Checked Then
                        totalCost =totalCost + wranglerCost + totalmultdrivers * sngDays + 52


                        'use if.......



this is because we have different checkbox and the user may select one or both of them so we have to consider all!

if this works dont forget to click on + sign below
Was This Post Helpful? 1
  • +
  • -

#5 lmill616  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 21-September 10

Re: code is not calculating right

Posted 21 September 2010 - 08:57 AM

View Postsmohd, on 21 September 2010 - 07:26 AM, said:

Ok the error is that you are required to use nested if instead of if..elseif

the correction is:
 'all following if will be in the first if then
                    If radNodrivers.Checked And radNoGas.Checked Then
                        totalCost = wranglerCost * sngDays
                   'put here
                   endif

                        'use if and change to
                    If radNodrivers.Checked And radYesGas.Checked Then
                        totalCost = totalCost + wranglerCost * sngDays + 52
                        endif

                        'use if and change to
                    If radYesdrivers.Checked And radYesGas.Checked Then
                        totalCost =totalCost + wranglerCost + totalmultdrivers * sngDays + 52


                        'use if.......



this is because we have different checkbox and the user may select one or both of them so we have to consider all!

if this works dont forget to click on + sign below



Thanks. I also had to move the code totalmultdrivers = sngdays * 22 into the if statement as well.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1