Do Until Loop

prime and exit a loop

Page 1 of 1

6 Replies - 17326 Views - Last Post: 19 September 2009 - 08:15 AM Rate Topic: -----

#1 penguindesktop  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 10-May 08

Do Until Loop

Post icon  Posted 06 June 2008 - 12:50 PM

Greetings All, I have a problem with a Do While loop. The following code is what I have so far. My issue is with entering the loop, and exiting. The user has to enter a sentinel value (-1) to exit the loop and tally up the values. I have that part working. What I can't figure out is how to where to put the console.read () for the user to enter "-1". I believe I should probably add another variable for the sentinel value since "tank" is what I use to count the # of tank of gas the user enters. Also, how do I check for division by zero. Currently, if I put in a 0 for gallons, the program crashes. Is is plausable to put a IF statement to check for zero input? Thanks for any advice in advance.

Richard


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()

         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


This post has been edited by PsychoCoder: 06 June 2008 - 01:07 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Do Until Loop

#2 mensahero  Icon User is offline

  • I Desire...
  • member icon

Reputation: 17
  • View blog
  • Posts: 678
  • Joined: 26-May 08

Re: Do Until Loop

Posted 06 June 2008 - 08:33 PM

Another good candidate to the vb.net forum.. :blink: :blink: ...

hello.. you must put your user input "console.read" <<IDK vb.net.. inside the do loop.. :blink:

do Until tank = -1
console.read
....

So that everytime the loop run.. the console.read method is invoke.. :blink:
Was This Post Helpful? 0
  • +
  • -

#3 penguindesktop  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 13
  • Joined: 10-May 08

Re: Do Until Loop

Posted 07 June 2008 - 08:23 AM

Thanks for the help.

What is "Another good candidate to the vb.net forum.." is that another good resource for learning VB?
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

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

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

Re: Do Until Loop

Posted 07 June 2008 - 08:44 AM

Moved to VB.NET :)
Was This Post Helpful? 0
  • +
  • -

#5 robertelder  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 30
  • Joined: 06-June 08

Re: Do Until Loop

Posted 09 June 2008 - 06:16 AM

penguindesktop,

Since your question about the Do Loop was already answered by >>>>>>, I'll skip it and move on to your other questions.

You can use IF THEN logic to check for divide-by-zero input, but since I assume you would want the user to input a different value, i would probably use a WHILE...END WHILE loop. Of course you could also combine the two and it would probably look like this:

	   Do Until tank = -1

			'...

			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

			'...

		Loop




As for your question about "Another good candidate to the vb.net forum..", DreamInCode has forums for several different programming languages. The questions about code written in C# should be posted in the C# forum... programmers more familiar with the C# forum will be better able to help out than a programmer who deals mainly with Visual Basic.

There are two forums at DreamInCode that deal with the Visual Basic language, and they are unfortunately often confused. With the 2003 release of Visual Basic (aka "VB7", "VB2003", and "VB 2003.Net"), VB started using the .Net Framework and it became much more powerful than prior versions (like VB6) and the language changed quite a bit. All releases since also include the .Net Framework.

A lot of programmers still use VB6, but because the .Net versions of VB are so different from VB6 and earlier versions, questions about code in VB6 and prior should go into the "VISUAL BASIC" forum, and questions about code from any of the .Net verstions of VB (2003, 2005, 2008) should go into the "VB.NET" forum.

Hope this helps you out!

-Rob
Was This Post Helpful? 0
  • +
  • -

#6 born2c0de  Icon User is offline

  • printf("I'm a %XR",195936478);
  • member icon

Reputation: 180
  • View blog
  • Posts: 4,667
  • Joined: 26-November 04

Re: Do Until Loop

Posted 10 June 2008 - 08:59 AM

@mensahero:
Please don't quote the complete first post when yours is #2.
It's understood whom your post is directed to.

*QUOTE REMOVED*
Was This Post Helpful? 0
  • +
  • -

#7 nevinkat  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 14-September 09

Re: Do Until Loop

Posted 19 September 2009 - 08:15 AM

This was a great help to me, but the instructions in my question were to use a sentinel value of -1.0 for the miles driven to terminate repetition (not # of tanks). So my code looks like this and works perfectly.

Module MilesPerGallon

	Sub Main()
		Dim milesDriven As Double 'miles driven per tankful
		Dim galUsed As Double 'gallons used per tankful
		Dim totalMiles As Double = 0 'total of miles driven
		Dim totalGallons As Double = 0 'total gallons used
		Dim average As Double 'average miles per gallon
		Dim tank As Integer = 0 'count of tankfuls

		Console.WriteLine("Please answer the following questions for each tank of gas.")
		Console.WriteLine("Use -1 for miles driven when all records have been entered.")
		Console.WriteLine()
		Console.WriteLine("Enter number of miles driven: ")
		milesDriven = Console.ReadLine()
		Do Until milesDriven = -1
			tank += 1
			Console.WriteLine("Enter number of gallons used:  ")
			galUsed = Console.ReadLine()
			Console.WriteLine()

			If galUsed = 0 Then
				While galUsed = 0
					Console.Write("0 is not a valid number.")
					Console.WriteLine()
					Console.Write("Enter number of gallons used:  ")
					galUsed = Console.ReadLine()
				End While
			End If

			totalMiles = milesDriven + totalMiles
			totalGallons = galUsed + totalGallons

			Console.WriteLine("The average mpg for tank # " & tank & " is " & milesDriven / galUsed)
			Console.WriteLine()

			Console.WriteLine("Enter number of miles driven:  ")
			milesDriven = Console.ReadLine()
			Console.WriteLine()

		Loop
		average = totalMiles / totalGallons
		Console.WriteLine("The total miles driven was:  " & totalMiles)
		Console.WriteLine("The total gallons used was:  " & totalGallons)
		Console.WriteLine("The total number of tanksful was:  " & tank)
		Console.WriteLine("Your average miles per gallon was:  " & average)

		Console.WriteLine("Thank you for your participation.")
		Console.Write("Press any key to close this screen.")
		Console.ReadLine()

	End Sub

End Module



Thanks for being here!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1