do until loop

total miles driven will not accumulate

Page 1 of 1

13 Replies - 1660 Views - Last Post: 18 June 2008 - 11:23 AM Rate Topic: -----

#1 mikeja76  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 17-June 08

do until loop

Post icon  Posted 17 June 2008 - 08:52 AM

i have to write a program to total tanks of gas as well as gas mileage. at the end i need to total the gas as well as the mileage. i also have to use a sentinel value of -1 to end the program. this is where the problem is. when i put -1 in the program does not terminate. instead it continues in th loop and treats it like a normal process. is there something that is missing or in the wrong spot??
any help will be appreciated. thanks

here is the code:
Module MPG

Sub Main()

Dim milesPerGallon As Double = 0 'miles driven per tankful 
Dim gallonsPerTank As Double = 0 'gallons used per tankful 
Dim gallonsUsed As Double = 0 'running total of gallons used 
Dim milesDriven As Double = 0 'running total of miles driven 
Dim average As Double ' the average miles per gallon 
Dim tank As Integer = 0 ' this counts how many tanks the user inputs 

Console.WriteLine("Input the requested information for each tankful")
Console.WriteLine("Use -1 to terminate the application")
Console.WriteLine()
Console.WriteLine()

Do Until tank = -1

Console.WriteLine("Input gas used and mile driven on this tankful")

Console.Write("How many Gallons did you use in this tank? ")
gallonsUsed = Console.ReadLine()

If gallonsUsed = 0 Then
While gallonsUsed = 0
Console.Write("""0"" is not a valid number for amount of gallons used.")
Console.Write("How many Gallons did you use in this tank? ")
gallonsUsed = Console.ReadLine()
End While
End If


Console.Write("How many miles did you drive on this tank? ")
milesDriven = Console.ReadLine()
tank += 1


milesPerGallon = milesDriven + milesPerGallon
gallonsPerTank = gallonsUsed + gallonsPerTank
Console.WriteLine("This is tank # " & tank)

Loop

average = milesPerGallon / gallonsPerTank
Console.WriteLine("Your average is {0:F}" & average & " Miles Per Gallon(mpg)")
Console.WriteLine("Your totals: Number of tanks: " & tank)
Console.WriteLine("Your total miles driven: " & milesDriven)
Console.WriteLine("Your total gallons used: " & gallonsPerTank)


End Sub

End Module


Is This A Good Question/Topic? 0
  • +

Replies To: do until loop

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8377
  • View blog
  • Posts: 31,135
  • Joined: 12-June 08

Re: do until loop

Posted 17 June 2008 - 09:02 AM

Tank is never 'set' a value from the input to be -1.. figure how tank becomes -1 (either by user input or some math count issue) and you are set.
Was This Post Helpful? 0
  • +
  • -

#3 mikeja76  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 17-June 08

Re: do until loop

Posted 17 June 2008 - 09:53 AM

still having an issue with this code. not sure what i need to do?? need more advice.. thanks
Was This Post Helpful? 0
  • +
  • -

#4 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: do until loop

Posted 17 June 2008 - 10:36 AM

mikeja76 I have few hints for you but I ain't coding it. :v: (As I believe you can do it.) :^:

View Postmikeja76, on 17 Jun, 2008 - 04:52 PM, said:

i have to write a program to total tanks of gas as well as gas mileage. at the end i need to total the gas as well as the mileage. i also have to use a sentinel value of -1 to end the program. this is where the problem is. when i put -1 in the program does not terminate. instead it continues in th loop and treats it like a normal process. is there something that is missing or in the wrong spot??
any help will be appreciated. thanks

here is the code:

Module MPG

Sub Main()

Dim milesPerGallon As Double = 0 'miles driven per tankful
Dim gallonsPerTank As Double = 0 'gallons used per tankful
Dim gallonsUsed As Double = 0 'running total of gallons used
Dim milesDriven As Double = 0 'running total of miles driven
Dim average As Double ' the average miles per gallon
Dim tank As Integer = 0 ' this counts how many tanks the user inputs

Console.WriteLine("Input the requested information for each tankful")
Console.WriteLine("Use -1 to terminate the application")
Console.WriteLine()
Console.WriteLine()


' Deleted Do Until tank = -1

Do Until False ' Loop Forever

Quote

Console.WriteLine("Input gas used and mile driven on this tankful")

Console.Write("How many Gallons did you use in this tank? ")
gallonsUsed = Console.ReadLine()

If gallonsUsed = 0 Then
While gallonsUsed = 0
Console.Write("""0"" is not a valid number for amount of gallons used.")
Console.Write("How many Gallons did you use in this tank? ")
gallonsUsed = Console.ReadLine()


Tasks
1) Insert a test condition here, if it's TRUE exit the loop
2) Find the second location where a similar statement is possibly required.

Quote

End While
End If


Console.Write("How many miles did you drive on this tank? ")
milesDriven = Console.ReadLine()
tank += 1
milesPerGallon = milesDriven + milesPerGallon
gallonsPerTank = gallonsUsed + gallonsPerTank
Console.WriteLine("This is tank # " & tank)

Loop

average = milesPerGallon / gallonsPerTank
Console.WriteLine("Your average is {0:F}" & average & " Miles Per Gallon(mpg)")
Console.WriteLine("Your totals: Number of tanks: " & tank)
Console.WriteLine("Your total miles driven: " & milesDriven)
Console.WriteLine("Your total gallons used: " & gallonsPerTank)

End Sub
End Module


Note: tank starts at 0 (Zero) and is only ever incremented. Eventually it would overflow before ever reaching -1.

Tip: When programming infinite loops, work out what the exiting conditions are, where they should be and program them first. This is especially important with recursive functions and subroutines

After you figure it out, post your answer.
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 8377
  • View blog
  • Posts: 31,135
  • Joined: 12-June 08

Re: do until loop

Posted 17 June 2008 - 11:17 AM

View PostAdamSpeight2008, on 17 Jun, 2008 - 12:36 PM, said:

mikeja76 I have few hints for you but I ain't coding it. :v: (As I believe you can do it.) :^:

View Postmikeja76, on 17 Jun, 2008 - 04:52 PM, said:

i have to write a program to total tanks of gas as well as gas mileage. at the end i need to total the gas as well as the mileage. i also have to use a sentinel value of -1 to end the program. this is where the problem is. when i put -1 in the program does not terminate. instead it continues in th loop and treats it like a normal process. is there something that is missing or in the wrong spot??
any help will be appreciated. thanks


After you figure it out, post your answer


Adam, mikeja will need to use the -1 input...

mikeja... your loop is this, right? "Do Until tank = -1"
You need that variable tank to be -1 at some point, and by the sounds of it by user input. The problem is you are using the wrong variable to check for user input..

In your code here (this is where the user would input gallons or -1 to exit, right?) should be examined.
Console.Write("How many Gallons did you use in this tank? ")
gallonsUsed = Console.ReadLine()


Hmmm.. let's think for a second.. if tank is not the right variable to check because it doesn't get user input... but gallonsused gets the user input including the -1 needed to end.. hmmm... what should the solution be?
Was This Post Helpful? 0
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: do until loop

Posted 17 June 2008 - 12:01 PM

modi123_1 re-examine the usage of Do Until tank = -1"

What would the result of the code following the exiting of the loop be?
Was This Post Helpful? 0
  • +
  • -

#7 mikeja76  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 17-June 08

Re: do until loop

Posted 17 June 2008 - 12:32 PM

thanks for the help. changing the variable made it work. now i have another problem with it. everything calculates right except for total miles driven. it gives me a -1. again, did i put something in the wrong place?? please give me some feed back. i really appreciate it. thanks again
Was This Post Helpful? 0
  • +
  • -

#8 mikeja76  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 17-June 08

Re: do until loop

Post icon  Posted 17 June 2008 - 01:02 PM

Module MPG

    Sub Main()

        Dim milesPerGallon As Double = 0 'miles driven per tankful  
        Dim gallonsPerTank As Double = 0 'gallons used per tankful  
        Dim gallonsUsed As Double = 0 'running total of gallons used  
        Dim milesDriven As Double = 0 'running total of miles driven  
        Dim average As Double ' the average miles per gallon  
        Dim tank As Integer = 0 ' this counts how many tanks the user inputs  


        Console.WriteLine("Input the requested information for each tankful")
        Console.WriteLine("Use -1 to terminate the application")
        Console.WriteLine()
        Console.WriteLine()

        Do Until gallonsUsed = -1

            Console.WriteLine("Input gas used and mile driven on this tankful")

            Console.Write("How many Gallons did you use in this tank? ")
            gallonsUsed = Console.ReadLine()

            If gallonsUsed = 0 Then
                While gallonsUsed = 0
                    Console.Write("""0"" is not a valid number for amount of gallons used.")
                    Console.Write("How many Gallons did you use in this tank? ")
                    gallonsUsed = Console.ReadLine()
                End While
            End If


            Console.Write("How many miles did you drive on this tank? ")
            milesDriven = Console.ReadLine()
            tank += 1



            milesPerGallon = milesDriven + milesPerGallon
            gallonsPerTank = gallonsUsed + gallonsPerTank
            Console.WriteLine("This is tank # " & tank)

        Loop


        average = milesPerGallon / gallonsPerTank

        Console.WriteLine("Your average is {0:F}" & average & " Miles Per Gallon(mpg)")
        Console.WriteLine("Your totals: Number of tanks: " & tank)
        Console.WriteLine("Your total miles driven: " & milesDriven)
        Console.WriteLine("Your total gallons used: " & gallonsPerTank)


    End Sub

End Module



Mod Edit: Please use code tags when posting your code => :code:
Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: do until loop

Posted 17 June 2008 - 01:08 PM

View Postmikeja76, on 17 Jun, 2008 - 08:32 PM, said:

thanks for the help. changing the variable made it work. now i have another problem with it. everything calculates right except for total miles driven. it gives me a -1. again did i put something in the wrong place?? please give me some feed back. i really appreciate it. thanks again


Depends on where you put it, ;) (Show us the code)
' Edited after merge
Wrong place.

If the loop is going loop for ever (possible) or at least until the user enters -1., does the checking of the sentinal value of -1 have to be combined with the DO Until?

They go
1) Between lines 29 & 30
2) Between lines 35 & 36

TotalMiles = Sum of all the separate Individual trip miles.

This post has been edited by AdamSpeight2008: 17 June 2008 - 01:27 PM

Was This Post Helpful? 0
  • +
  • -

#10 jacobjordan  Icon User is offline

  • class Me : Perfection
  • member icon

Reputation: 113
  • View blog
  • Posts: 1,499
  • Joined: 11-June 08

Re: do until loop

Posted 17 June 2008 - 01:10 PM

Problem is not clearly stated, but i do see one critical flaw:

After the line that says
Console.WriteLine("Your total gallons used: " & gallonsPerTank)


You need to add this line:
Console.ReadLine()


If this is not done, your application will automatically terminate.
Was This Post Helpful? 0
  • +
  • -

#11 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1633
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: do until loop

Posted 17 June 2008 - 01:10 PM

You have a separate thread with the same exact code and same exact title. Do not create duplicate topics. MERGED!
Was This Post Helpful? 0
  • +
  • -

#12 mikeja76  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 17-June 08

Re: do until loop

Posted 17 June 2008 - 01:21 PM

Module MPG

    Sub Main()

        Dim milesPerGallon As Double = 0 'miles driven per tankful  
        Dim gallonsPerTank As Double = 0 'gallons used per tankful  
        Dim gallonsUsed As Double = 0 'running total of gallons used  
        Dim milesDriven As Double = 0 'running total of miles driven  
        Dim average As Double ' the average miles per gallon  
        Dim tank As Integer = 0 ' this counts how many tanks the user inputs  


        Console.WriteLine("Input the requested information for each tankful")
        Console.WriteLine("Use -1 to terminate the application")
        Console.WriteLine()
        Console.WriteLine()

        Do Until gallonsUsed = -1

            Console.WriteLine("Input gas used and mile driven on this tankful")

            Console.Write("How many Gallons did you use in this tank? ")
            gallonsUsed = Console.ReadLine()

            If gallonsUsed = 0 Then
                While gallonsUsed = 0
                    Console.Write("""0"" is not a valid number for amount of gallons used.")
                    Console.Write("How many Gallons did you use in this tank? ")
                    gallonsUsed = Console.ReadLine()
                End While
            End If


            Console.Write("How many miles did you drive on this tank? ")
            milesDriven = Console.ReadLine()
            tank += 1



            milesPerGallon = milesDriven + milesPerGallon
            gallonsPerTank = gallonsUsed + gallonsPerTank
            Console.WriteLine("This is tank # " & tank)

        Loop


        average = milesPerGallon / gallonsPerTank

        Console.WriteLine("Your average is {0:F}" & average & " Miles Per Gallon(mpg)")
        Console.WriteLine("Your totals: Number of tanks: " & tank)
        Console.WriteLine("Your total miles driven: " & milesDriven)
        Console.WriteLine("Your total gallons used: " & gallonsPerTank)
        Console.ReadLine()

    End Sub

End Module



Mod Edit: Once again, please use code tags when posting your code => :code:
Was This Post Helpful? 0
  • +
  • -

#13 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: do until loop

Posted 17 June 2008 - 01:42 PM

See POST 9

What could happen if the user enters -1 on the first tank?

This post has been edited by AdamSpeight2008: 17 June 2008 - 01:48 PM

Was This Post Helpful? 0
  • +
  • -

#14 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2216
  • View blog
  • Posts: 9,352
  • Joined: 29-May 08

Re: do until loop

Posted 18 June 2008 - 11:23 AM

mikeja76, this is how I would go about solving it.

State the problem.

Quote

i have to write a program to total tanks of gas as well as gas mileage. at the end i need to total the gas as well as the mileage. i also have to use a sentinel value of -1 to end the program.


Decomposition of the problem. (Break the problem down into smaller problems.)

1. How will the user input the information?
2 How many times while user input the information?
3. How do i Test for valid inputs?
4. How do I calculate the Total Tanks of gas?
5. How do I calculate the Total Gas Millage?
6. How do test for the sentinel value?
7. How do I display the result?

Answering the questions.

1. Via the console
2. Until they enter a sentinal value of -1
3. For Gallons it must be greater than zero
For Miles it must be greater than zero.
4. Totals Tanks equates to number of times the user enter valids inputs for both miles and gallons
Ie Every time the user enter a valid info I will increment Tanks value by 1
5. GasTotal = All the gallons entered added together.
MileTotal = All the Miles entered added together.

How can this done?
Keep a running total for miles and Gas
GasTotal = GasTotal + Gallons
MilesTotal = MilesTotal + Miles

6. If either Gallons_Entered or Miles_Enter is -1
7. back to console.

Look back at the answers do they require more decomposition?
Thinking about it, answer 1: via the console.
Do the console only let the user input numbers?
No, so I'll need to test for this.

Here one version in Pseudo code

SET Sentinel_Value TO -1
SET Sentinel_Trigger TO FALSE
DO UNTIL Sentinel_Trigger
 COMMENT "Get user input for Gallons"
 SET Valid_Input TO FALSE
 WHILE Valid_Input IS FALSE
  GET User_Input
  COMMENT "Could the user enter something other than a number?"
  IF User_Input IS Number THEN
   IF User_Input IS Sentinel_Value THEN
	SET Valid_Input TO TRUE
	SET Sentinel_Triggered TO TRUE
   ELSE
	IF User_Input WITHIN RANGE ALLOWED THEN
	 SET Valid_Input TO TRUE
  END IF
 END WHILE
 COMMENT "Valid input for Gallons"
 IF Sentinel_Triggered IS FALSE THEN
   COMMENT "Get Second User Input, (Miles) is it valid etc"
 END IF
 IF Sentinel_Triggered IS FALSE THEN
  ' Do processing on valid inputs
 END IF
LOOP
' Display results of processing



This can be rewritten

SET Sentinel_Value TO -1
SET Sentinel_Triggered TO FALSE
DO 
   SET Valid_Input TO FALSE
 WHILE Valid_Input IS FALSE
  GET User_Input
  COMMENT "Could the user could input something other than numbers?"
  IF User_Input IS VALID  THEN
   IF User_Input IS Sentinel_Value THEN
	SET Valid_Input TO TRUE
	SET Sentinel_Triggered TO TRUE
   ELSE
	IF User_Input WITHIN RANGE ALLOWED THEN
	 SET Valid_Input TO TRUE
  END IF
 END WHILE
 ' Now have valid input
 IF Sentinel_Triggered IS FALSE THEN
   ' Get Second User Input, is it valid etc
 END IF
 IF Sentinel_Triggered IS FALSE THEN
  ' Do processing on valid inputs
 END IF
UNTIL Sentinel_Triggered IS TRUE
' Display results of processing



Which can be simplified further.
SET Sentinel_Value To -1
COMMENT "Loop #1" Loop Forever (Until the user tells me to end asking)
DO UNTIL FALSE
 COMMENT "Loop # 2" Loop Forever ( until the user input are valid)
 WHILE TRUE
  GET User_Input
  COMMENT "Could the user enter something other then a number?"
  IF User_Input IS Number Then
   IF User_Input IS Sentinel_Value THEN
	COMMENT "This will exit LOOP 1, thus end asking for inputs."
	EXIT DO
   ELSE
	IF User_Input IS WITHIN RANGE ALLOWED THEN
	 COMMENT "This will exit LOOP 2, thus the user has entered something valid "
	 EXIT WHILE
	END IF  
  END IF
 END WHILE
 ' Get Second Valid User Input
 ' After getting both valid Input do processing on them
LOOP
' Display valid results


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1