Reading file help – Only displays one line.

Hello, I am currently writing an arithmetic quiz code for a school project, where scores are stored like this: My aim is to read the file, and to sort them into alphabetical, highest to lowest, and an average of each score for each person. My difficulty comes in trying to show all 3 names and their averages, whilst the code only shows one. My code is as follows:

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO;

namespace ConsoleApplication14 { class Program { static void Main(string[] args) { { start: Dictionary<string, int> Max =//Creates a dictionary new Dictionary<string, int>(); Dictionary<string, int> Average = new Dictionary<string, int>();

 string line; string[] scores; int highest; Console.WriteLine("What class are you in? 1, 2 or 3?"); string classe = Console.ReadLine(); Console.WriteLine("How would you like to sort the scores? Enter the corresponding letter to sort."); Console.WriteLine("Alphabetically(L)"); Console.WriteLine("Highest Score(H)"); Console.WriteLine("Average Score(V)"); string sort = Console.ReadLine(); using (StreamReader reader = new StreamReader("G:Class" +(classe)+ "Scores.txt"))//Locates the text file { while (!reader.EndOfStream) { line = reader.ReadLine();//Splits into lines scores = line.Split(); highest = Convert.ToInt32(scores.Skip(1).Max());//Skips the name to get 3 scores Max.Add(scores[0], highest); int average = (Convert.ToInt32(scores[1]) + Convert.ToInt32(scores[2]) + Convert.ToInt32(scores[3])) / 3;//Works out the average Average.Add(scores[0], average); } if (sort == "H")//Reads decision and sorts by the highest score { foreach (var item in Max.OrderBy(i => i.Value)) { Console.WriteLine(item); goto start; } } else if (sort == "V")//Reads decision and sorts by the average score { foreach (var item in Average.OrderBy(i => i.Value)) { Console.WriteLine(item); goto start; } } else if (sort == "L")//Reads decision and sorts by the name { var list = Max.Keys.ToList(); list.Sort(); foreach (var key in list) { Console.WriteLine("{0}: {1}", key, Max[key]); goto start; } } else goto start; } } } } 


Genuinely thank you in advance for any help with this problematic task.

by LewasBettle via /r/csharp

Leave a Reply