what I am trying to do is use a try-catch exception to restart the program and get the user to reeter the data values again. How would I do this? I tried using a goto to bring it back to the first line but this didn’t seem to work. (and the general consensus is that goto’s are evil). Any help that could be given greatly appreciated.
Console.WriteLine("Please enter the two points that you wish to know the distance between:");
string point = Console.ReadLine();
string[] pointInput = point.Split(' ');
int pointNumber = Convert.ToInt16(pointInput[0]); //Stores the actual input number's into two integers
int pointNumber2 = Convert.ToInt16(pointInput[1]);
try //Try-Catch statement to make sure that the User enters relevant PointNumbers
{
double latitude = (Convert.ToDouble(items[pointNumber * 3])); //
double longtitude = (Convert.ToDouble(items[(pointNumber * 3) + 1])); //
double elevation = (Convert.ToDouble(items[(pointNumber * 3) + 2])); //
double latitude2 = (Convert.ToDouble(items[pointNumber2 * 3])); //
double longtitude2 = (Convert.ToDouble(items[(pointNumber2 * 3) + 1])); //
double elevation2 = (Convert.ToDouble(items[(pointNumber2 * 3) + 2])); // Uses the relationship between the pointnumber and the array to select the required items from the array.
//Calculate the distance between two point using the Distance class
Console.WriteLine("The distance in km's to two decimal places is:");
Distance curDistance = new Distance(latitude, longtitude, elevation, latitude2, longtitude2, elevation2);
Console.WriteLine(String.Format("{0:0.00}", curDistance.toDistance()) + "km");
}
catch(IndexOutOfRangeException)
{
Console.WriteLine("You have selected a point number outside the range of the data entered, please select two new pointnumbers");
// here is where I would have the program restart
}
When you start learning to program you learn that these situations can be solved using while or do-while loops. Therefore I will give you such answer:
Watch out that if you call Main within Main you can end up making StackOverflowException 😀