943,882 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Marked Solved
  • Views: 1432
  • VB.NET RSS
Oct 28th, 2006
0

Help with a problem

Expand Post »
Enter the miles driven, use -1 to end:
Enter the gallons :

The miles / gallon for this tank was

Enter the miles driven, use -1 to end:
Enter the gallons used (-1 to end) :


The miles / gallon for this tank was


Enter the miles driven, use -1 to end:
Enter the gallons used (-1 to end) :

The miles / gallon for this tank was
Enter the miles driven, use -1 to end: -1

The overall average miles/gallon was

I tried to solve this above probem but i cant figure out where i m going wrong or what to do next .. can anyone please help me out with it

VB.NET Syntax (Toggle Plain Text)
  1. Public Sub OverallAverage()
  2.  
  3.  
  4. Dim miles, gallons As Double
  5. Dim totalGallons As Double = 0
  6. Dim totalMiles As Double = 0
  7. Dim tank As Double = 0
  8. Dim average As Double = 0
  9.  
  10.  
  11. Console.Write("Enter the miles driven, use -1 to end: ")
  12. gallons = Console.ReadLine()
  13.  
  14. While miles <> -1
  15.  
  16. Console.Write("Enter the gallons: ")
  17. gallons = Console.ReadLine()
  18.  
  19. tank = miles / gallons
  20. Console.WriteLine("The miles / gallon for this tank was " & tank)
  21.  
  22. Console.WriteLine("Enter the miles driven, use -1 to end: ")
  23. miles = Console.ReadLine()
  24.  
  25. totalGallons = totalGallons + gallons
  26. totalMiles = totalMiles + miles
  27.  
  28. End While
  29.  
  30. average = totalMiles / totalGallons
  31. Console.Write("The overall average miles/gallon was: " & average)
  32.  
  33. End Sub
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vickzbrit is offline Offline
14 posts
since Mar 2006
Oct 28th, 2006
0

Re: Help with a problem

A couple of things:
        'you had this as gallons should be miles
        miles = Console.ReadLine()
and
            'move this to after calculate totals otherwise changing miles 
before adding to total
            'miles = Console.ReadLine()

            totalGallons = totalGallons + gallons
            totalMiles = totalMiles + miles
            miles = Console.ReadLine()
Reputation Points: 84
Solved Threads: 58
Posting Pro in Training
waynespangler is offline Offline
461 posts
since Dec 2002
Oct 28th, 2006
0

Re: Help with a problem

Thanks for pointing me out waynespangler.. i made the change for the first one you pointed me out.. but for the second one.. I am confused. If i remove

VB.NET Syntax (Toggle Plain Text)
  1. miles=console.Readline()

from the top and move it to the bottom after my calculations, it asks for gallons before miles.

And to quite my program when entered -1, I have added the following code
VB.NET Syntax (Toggle Plain Text)
  1. If miles >= 0 Then
  2.  
  3. average = totalMiles / totalGallons
  4. Console.WriteLine("The overall average miles/gallon was: " & average)
  5. Else
  6. Console.WriteLine("You have chose to quite by pressing -1")
  7.  
  8. End If

now.. when pressed -1, It should also show The overall average miles/gallon. I am not sure where did i make a mistake
Last edited by vickzbrit; Oct 28th, 2006 at 9:05 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vickzbrit is offline Offline
14 posts
since Mar 2006
Oct 29th, 2006
0

Re: Help with a problem

alright .. i figured it out .. thankz for helping me out waynespangler
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vickzbrit is offline Offline
14 posts
since Mar 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: need help
Next Thread in VB.NET Forum Timeline: key in data in console...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC