Warning! It seems that you are using Dodona within another webpage, so not everything may work properly. Let your teacher know so that he can solve the problem by adjusting a setting in the learning environment. In the meantime, you can click this link to open Dodona in a new window.
Warning! The page was not fully loaded, probably because of a network issue. It could be that not all functionalities work as expected. Please try reloading the page.
Csharp exercise
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();
}
}