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");
}
}