954,123 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help with a problem

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

Public Sub OverallAverage()


        Dim miles, gallons As Double
        Dim totalGallons As Double = 0
        Dim totalMiles As Double = 0
        Dim tank As Double = 0
        Dim average As Double = 0


        Console.Write("Enter the miles driven, use -1 to end: ")
        gallons = Console.ReadLine()

        While miles <> -1

            Console.Write("Enter the gallons: ")
            gallons = Console.ReadLine()

            tank = miles / gallons
            Console.WriteLine("The miles / gallon for this tank was " & tank)

            Console.WriteLine("Enter the miles driven, use -1 to end: ")
            miles = Console.ReadLine()

            totalGallons = totalGallons + gallons
            totalMiles = totalMiles + miles

        End While

        average = totalMiles / totalGallons
        Console.Write("The overall average miles/gallon was: " & average)

    End Sub
vickzbrit
Newbie Poster
14 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

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()
waynespangler
Posting Pro in Training
461 posts since Dec 2002
Reputation Points: 84
Solved Threads: 58
 

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

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

If miles >= 0 Then

            average = totalMiles / totalGallons
            Console.WriteLine("The overall average miles/gallon was: " & average)
        Else
            Console.WriteLine("You have chose to quite by pressing -1")

        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

vickzbrit
Newbie Poster
14 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

alright .. i figured it out :) .. thankz for helping me out waynespangler

vickzbrit
Newbie Poster
14 posts since Mar 2006
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You