Drop hier links of afbeeldingen om ze aan de editor toe te voegen.

Sign up for a school club.

School club

Try

Watch this video to learn about the new concepts shown in the program:

Knowledge organiser

string line = reader.ReadLine();

line is assigned to be a single line read from StreamReader reader. The data read will include the end-of-line character code, so use .Trim() to remove it. ReadLine returns null at the end of the file, so it can be used to read a file one line at a time: while ((line = reader.ReadLine()) != null).

StreamWriter writer = new StreamWriter(path, true);

Opens path for writing. The second argument true means new data is appended to the end of the file instead of overwriting what is already there (use false to overwrite). Write a line with writer.WriteLine(data); and call writer.Close(); when finished.

Data in text files is often stored in comma separated value (CSV) format. E.g.

"red",255,0,0
"green",0,255,0
"blue",0,0,255

A single line is one record with each field of the record separated with a comma. Using the data above and the following code:

string record = reader.ReadLine().Trim();
string[] fields = record.Split(",");

Would result in:

fields[0] is "red"
fields[1] is 255
fields[2] is 0
fields[3] is 0

Investigate

Questions to think about with this program to check your understanding:

Purpose question

What is the purpose of the iteration:

while ((line = file.ReadLine()) != null)
Reveal answer

To read in the students line by line from the beginning to the end of the file.

Reason question

Why is the file opened in append mode (new StreamWriter(club + ".txt", true)) instead of overwrite mode when signing up a new student?

Reveal answer

New students need to be added to the file. Appending means that new data is added to the end and does not over-write existing data in the file.

Make

Change the program so that it:

  1. Asks the user for the name of the club before adding or outputting the students. The club can only be programming, football or drama.
  2. Writes and reads a different file for each club. The name of the club is the filename. E.g. football.txt would be the football club.

Typical inputs and outputs from the program would be:

1. Sign up
2. Show students
Enter choice:
1
Clubs available: programming, football or drama
programming
Enter your name to sign up for the club:
Craig
You have been signed up Craig

1. Sign up
2. Show students
Enter choice:
2
Clubs available: programming, football or drama
football
Students that signed up for the football club:
Dave
Sam
Mo
🆘 If you're really stuck, use this Parsons code sorting exercise
add_student
// Add a student to the club
static void add_student(string club)
{
---
    Console.WriteLine("Enter your name to sign up for the club:");
    string student = Console.ReadLine();
---
    StreamWriter file = new StreamWriter(club + ".txt", true);
    file.WriteLine(student);
    file.Close();
---
    Console.WriteLine($"You have been signed up {student}\n");
}
show_students
// Show the students who signed up for a club
static void show_students(string club)
{
---
    Console.WriteLine($"Students that signed up for the {club} club:");
    try
    {
---
        List students = new List();
        StreamReader file = new StreamReader(club + ".txt");
        string line;
        while ((line = file.ReadLine()) != null)
        {
            students.Add(line.Trim());
        }
        file.Close();
---
        foreach (string student in students)
        {
            Console.WriteLine(student);
        }
    }
---
    catch (FileNotFoundException)
    {
        Console.WriteLine("No students have signed up yet.");
    }
}
</pre>
</dw-parsons-puzzle>

menu
// Menu
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();
---
    }
---
    Console.WriteLine("Clubs available: programming, football or drama");
---
    string club = "";
    while (club != "programming" && club != "football" && club != "drama")
    {
---
        club = Console.ReadLine();
---
    }
---
    switch (choice)
    {
---
        case "1":
            add_student(club);
            break;
        case "2":
            show_students(club);
            break;
---
    }
---
}
</details>