Level 8 - Files and exceptions 0 of 9 activities solved
  1. Saving data Not yet solved
  2. School club Not yet solved
  3. Shopping list Not yet solved
  4. Attributes Not yet solved
  5. Cookies Not yet solved
  6. Morse code Not yet solved
  7. ini file Not yet solved
  8. Amino acids Not yet solved
  9. Gamertag Not yet solved
Next series Level 9 - Two-dimension arrays and lists
Level 8 - Files and exceptions

School club

Sign in to hand in your solution and get feedback.

// School club program using System; using System.IO; using System.Collections.Generic; class Submission { // ------------------------- // Subprograms // ------------------------- static void add_student() { StreamWriter file = new StreamWriter("programming.txt", true); Console.WriteLine("Enter your name to sign up for the club:"); string student = Console.ReadLine(); file.WriteLine(student); file.Close(); Console.WriteLine($"You have been signed up, {student}"); } static void show_students() { Console.WriteLine($"Students that signed up for the programming club:"); List<string> students = new List<string>(); StreamReader file = new StreamReader("programming.txt"); string line; while ((line = file.ReadLine()) != null) { students.Add(line.Trim()); } file.Close(); foreach (string student in students) { Console.WriteLine(student); } } static void menu() { Console.WriteLine("1. Sign up"); Console.WriteLine("2. Show students"); string choice = ""; while (choice != "1" && choice != "2") { Console.WriteLine("Enter choice:"); choice = Console.ReadLine(); } switch (choice) { case "1": add_student(); break; case "2": show_students(); break; } } // ------------------------- // Main program // ------------------------- public static void Main(string[] args) { menu(); } }