0

for some reason this will compile but it comes out with an error at the end and I can't figure out why. The first part of the code is to display a table from a text file which works correctly, the second part doesn't.

I don't think it even gets to the Console.WriteLine bit, which was a way of checking whether it did. Can anyone see why?

Thanks for any help you can give!

class Program
{
    static void Main(string[] args)

    {
        List<float> inputList = new List<float>();
        TextReader tr = new StreamReader("c:/users/tom/documents/visual studio 2010/Projects/DistanceCalculator3/DistanceCalculator3/TextFile1.txt");
        String input = Convert.ToString(tr.ReadToEnd());
        String[] items = input.Split(',');
        Console.WriteLine("Point         Latitude        Longtitude       Elevation");

        for (int i = 0; i < items.Length; i++)
        {
            if (i % 3 == 0)
            {
                Console.Write((i / 3) + "\t\t");
            }

            Console.Write(items[i]);
            Console.Write("\t\t");

            if (((i - 2) % 3) == 0)
            {
                Console.WriteLine();
            }

        }

        Console.WriteLine();
        Console.WriteLine();

        // Ask for two bits of data which are then stored in Longtitude, Latitude and Elevation 

        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]);
        int pointNumber2 = Convert.ToInt16 (pointInput[1]);

        int Latitude = (Convert.ToInt16(items[pointNumber*3]));
        int Longtitude = (Convert.ToInt16(items[(pointNumber*3)+1]));
        int Elevation = (Convert.ToInt16(items[(pointNumber*3)+2]));

        int Latitude2 = (Convert.ToInt16(items[pointNumber2 * 3]));
        int Longtitude2 = (Convert.ToInt16(items[(pointNumber2 * 3) + 1]));
        int Elevation2 = (Convert.ToInt16(items[(pointNumber2 * 3) + 2]));

        Console.WriteLine("Latitude");
        Console.WriteLine("Latitude2");
13
  • 3
    Please include a stack trace. Please also do this for all runtime errors you encounter in the future for any question. Please also mark relevant line numbers. Commented Oct 24, 2012 at 17:07
  • debug your program and put break points where necessary Commented Oct 24, 2012 at 17:08
  • And since you're doing a bunch of Console.Writeing, it would help to also include this output along with your expected output for a successful run. Commented Oct 24, 2012 at 17:09
  • Show TextFile1.txt, else we don't know what you are parsing.. Commented Oct 24, 2012 at 17:11
  • 1
    Ah! I've got it working! Thanks guys!! Whoever mentioned the float is a hero, and the fact that there is no Longtitude specified was right!! <3 Heros! Commented Oct 24, 2012 at 17:19

2 Answers 2

1

You are using decimal values, which cannot be converted into Int16. So use float.

Also, Outputting "Latitude" will write the variable's name, not its value. I modified your code:

float Latitude = (float.Parse(items[pointNumber*3]));
float Longtitude = (float.Parse(items[(pointNumber*3)+1]));
float Elevation = (float.Parse(items[(pointNumber*3)+2]));

float Latitude2 = (float.Parse(items[pointNumber2 * 3]));
float Longtitude2 = (float.Parse(items[(pointNumber2 * 3) + 1]));
float Elevation2 = (float.Parse(items[(pointNumber2 * 3) + 2]));

Console.WriteLine(Latitude);
Console.WriteLine(Latitude2);
Sign up to request clarification or add additional context in comments.

Comments

1

Your input strings are of floating values, not integers. You can parse to floats like this:

float Latitude = (Convert.ToSingle(items[pointNumber * 3]));
float Longtitude = (Convert.ToSingle(items[(pointNumber * 3) + 1]));
float Elevation = (Convert.ToSingle(items[(pointNumber * 3) + 2]));

float Latitude2 = (Convert.ToSingle(items[pointNumber2 * 3]));
float Longtitude2 = (Convert.ToSingle(items[(pointNumber2 * 3) + 1]));
float Elevation2 = (Convert.ToSingle(items[(pointNumber2 * 3) + 2]));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.