C# Console App

Associate
Joined
7 Aug 2008
Posts
302
Hey Lads.

I'm currently programming or trying to program a C# console app that reads in a number of records from a text file line by line and I am trying to store the data into an array.

Now I should be able to type N and it goes to the next record, P and it goes to the previous however I can't find a way to be able to increment the record when the N button is entered or decrement it when the P button is entered. I think once I find out how to do this, the Last and First commands should be simple.

Any ideas what I could use?

Cheers.
 
At the moment I am using a case statement for the different commands but I don't think it's a good way of doing it.

I have a variable which is equal to the array; example = arrText.ToArray then using example - so for example fo display the first record it would be i = 0, then for next i++ however it will only go to the next record and if I press N again it stops as the counter has been reset back to 0 I think.

Don't know what the best way to do it is.
 
Code:
string[] scores = arrText.ToArray(typeof(string)) as string[];
                

                switch (what)
                {
                    case '1':
                        i = 0;
                        Console.WriteLine(scores[i]);
                        break;
                    case '2':
                        i++;
                        Console.WriteLine(scores[i]);
                        break;
                    case '3':
                        i--;
                        Console.WriteLine(scores[i]);
                        break;
                    case '4':
                        i = scores.Length - 1;
                        Console.WriteLine(scores[i]);
                        break;
                }
                Console.WriteLine();
 
I think I am moving away from this as I need to be able to perform Sorting on Dates as well.

How would I go about reading the records into a Class or a Structure?

i.e FirstName, Surname etc

Any suggestions on the best way of doing this would be appreciated.

Cheers.
 
I think I need to read the text file into the array and then sort of break this array into a structure;

For Example;

FirstName = [0]
Surname = [1]

That kind of thing?

Just haven't got a clue how I can go about this.
 
I have a few Strings, a DateTime, Char's all seperated by commas.

I have manged to split the data by commas so I have [0] representing one element, [1] representing the other etc and have set these up into some kind of structure.

However, I am now stuck on how to display the records line by line depending on what key has been pressed. I can get the data to display all lines however this is of no use to me.
 
Right cheers for that. Will have a proper look through it tomorrow.

The only reason I am using Console is because it is a requirement to. Trust me I would much rather use a GUI over it.

Thanks Again.
 
Right, I have split the data from the text file up into individual elements using the Split.Line and splitting it by commas.

Now I can do Person.Surname = parts[0] - meaning the first elements are the surnames, second names are the second etc.

Now when I WriteLine Person.FirstName, Person.Surname it displays all of the records as opposed to just one record. I need to set up some kind of index I think, i.e. if x is pressed display first record, if y is pressed display the next as opposed to it displaying all of the records.

Any idea how I can set up a counter, index for each individual record so I can display individual records as opposed to all the data?

Cheers
Tom.
 
Code:
        static void Main()
        {
            using (StreamReader reader = new StreamReader("data.txt"))
            {
                string line;
                int count = 0;
                while ((line = reader.ReadLine()) != null)
                {
                    //int lineCount = File.ReadAllLines("data.txt").Length;
                    //Console.WriteLine(lineCount);
                    count++;
                    string[] parts = line.Split(',');
                    Student Person;
                    Person.detail = parts[0];
                    Person.test = parts[1];
                    Person.detail = parts[2];
                    Person.test = DateTime.Parse(parts[3]);
                    Person.detail = Char.Parse(parts[4]);
                    Console.WriteLine(Person.detail + "\t" + Person.test + "\t" + Person.detail + "\t" + String.Format("{0:MM/dd/yyyy}", Person.test) + "\t" + Person.detail);
                    Console.WriteLine(parts);

No it isn't a homework question - just a task that I am doing.

So what I am trying to do now is to show the individual records as opposed to all of them at the same time. I think I need to use the counter somehow, maybe to incorporate it into some kind of array. Keep trying all sorts and just can't get it to work.
 
Back
Top Bottom