code is not calculating right

Page 1 of 1

4 Replies - 1018 Views - Last Post: 21 September 2010 - 08:57 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=191291&amp;s=a44f00a0f76e1db57d8daf7c1aca0ac4&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 lmill616

Reputation: 0
• 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 user chooses no multiple drivers and no prepaid gas
'calculates right
totalCost = wranglerCost * sngDays

'if user chooses no mult drivers and yes to prepaid gas
'does not calc right
totalCost = wranglerCost * sngDays + 52

'if user chooses yes to mult drivers and yes to prepaid gas
'does not calc right
totalCost = wranglerCost + totalmultdrivers * sngDays + 52

'if user chooses yes to mult drivers and no to prepaid gas
'does not calc right
totalCost = wranglerCost + totalmultdrivers * sngDays
End If

```

Is This A Good Question/Topic? 0

Replies To: code is not calculating right

#2 smohd

• Critical Section

Reputation: 1822
• Posts: 4,627
• 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

#3 lmill616

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

Re: code is not calculating right

Posted 21 September 2010 - 07:49 AM

smohd, 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
totalCost = wranglerCost * sngDays

'if user chooses no mult drivers and yes to prepaid gas
'does calc right
totalCost = wranglerCost * sngDays + 52

'if user chooses yes to mult drivers and yes to prepaid gas
'does not calc right
totalCost = wranglerCost + totalmultdrivers * sngDays + 52

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

```

#4 smohd

• Critical Section

Reputation: 1822
• Posts: 4,627
• 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
totalCost = wranglerCost * sngDays
'put here
endif

'use if and change to
totalCost = totalCost + wranglerCost * sngDays + 52
endif

'use if and change to
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

#5 lmill616

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

Re: code is not calculating right

Posted 21 September 2010 - 08:57 AM

smohd, 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
totalCost = wranglerCost * sngDays
'put here
endif

'use if and change to
totalCost = totalCost + wranglerCost * sngDays + 52
endif

'use if and change to
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.