0

I've working on a C# application that is supposed to input miles driven and gallons used(integers) for each tankful of gas and then the application should calculate and display the miles per gallon obtained for each tankful and display combined miles per gallon obtained for all tankfuls. My issue is that the sentinel controlled aspect "-1" doesnt stop program from running, yet, the IDE kicks out a JIT debugger and I get a Microsoft.NET framework exception MileageDriven.exe[732].

Can I have some assistance, as I dont want to move beyond this exercise as future lessons build upon the last lesson. Thanks in advance. :)

using System;

public class MileagedrivenTest
{
   public static void Main(string[] args)
   {
      Mileage driverMileage = new Mileage("Driver log");

      driverMileage.DisplayMessage();
      driverMileage.DetermineMileagePerGallon();
   } // end main
} // end class MileageDrivenTest

// Fig. 1.1: MileageDriven.cs
// Mileage class that solves mileage per gallon problem
// using sentinel-controlled repetition
using System;

public class Mileage
{
   public string Gallons { get; set; }

   public Mileage (string name)
   {
      Gallons = name;
   }

   public void DisplayMessage()
   {
      Console.WriteLine("Welcome to the mileage log for\n{0}\n", Gallons);
   } // end method DisplayMessage

   //determine the average mileage for gallon per tank of gas
   public void DetermineMileagePerGallon()
   {
      int total = 0;
      int mileage; // sum of miles driven
      int gallons; // sum gallons used
      double average;

      int mileageCounter = 0;

      Console.WriteLine("Enter mileage or -1 to quit: ");
      mileage = Convert.ToInt32(Console.ReadLine());
      Console.WriteLine("Enter gallons used: ");
      gallons = Convert.ToInt32(Console.ReadLine());

      while (mileage != -1)
      {
         mileage = total + mileage;
         mileageCounter = mileageCounter + mileage;

         Console.Write("Enter next miles driven or -1 to quit: ");
         mileage = Convert.ToInt32(Console.ReadLine());         
      } // end while

      if (mileageCounter != 0)
      {
         average = (double)total / mileageCounter;

         Console.WriteLine("\nTotal of the {0} miles driven is {1} per tank is{}", mileageCounter, total);
         Console.WriteLine("Average mile per gallon is {0}", average);
      }
      else
         Console.WriteLine("No mileage entered");      
   }
}
Rodney Davis
  • 1
  • 1
  • 3

1 Answers1

0

The code you posted throws an exception on this line:

Console.WriteLine("\nTotal of the {0} miles driven is {1} per tank is{}", mileageCounter, total);

The problem is the extra {}. If you remove that the code will run but I don't think that it solves the problem that you're trying to solve. I've modified DetermineMileagePerGallon() based on my interpretation of the problem - hopefully it'll get you closer to a solution:

 //determine the average mileage for gallon per tank of gas
   public void DetermineMileagePerGallon()
   {
       int totalTankfuls = 0;
       int inputMileage;
       int totalMileage = 0; // sum of miles driven
       int totalGallons = 0; // sum gallons used

       Console.WriteLine("Enter mileage or -1 to quit: ");
       while (!Int32.TryParse(Console.ReadLine(), out inputMileage)) ;

       while (inputMileage != -1)
       {
           totalTankfuls++;

           if (inputMileage > 0)
               totalMileage += inputMileage;

           Console.WriteLine("Enter gallons used: ");
           int inputGallons;
           while (!Int32.TryParse(Console.ReadLine(), out inputGallons)) ;

           if (inputGallons > 0)
               totalGallons += inputGallons;

           Console.WriteLine("Enter mileage or -1 to quit: ");
           while (!Int32.TryParse(Console.ReadLine(), out inputMileage)) ;

       } // end while

       if (totalTankfuls > 0)
       {
           double averageMpg = (double)totalMileage / totalGallons;

           double averageMileagePerTankful = (double)totalMileage / totalTankfuls;
           Console.WriteLine("Total of {0} miles driven on {1} tanks of fuel; average is {2} miles per tankful", totalMileage, totalTankfuls, averageMileagePerTankful);
           Console.WriteLine("Average mile per gallon across all tankfuls is {0}", averageMpg);
       }
       else
           Console.WriteLine("No mileage entered");
   }
Ian Gilroy
  • 2,031
  • 16
  • 14