'How can i repeat the condition if the user wants to input many student name in the list and how can i list all of them. after i input "N"

I only got the example of the program but how can i repeat the condition in order to get the desired output of the program. I'm confused on how I'm going to repeat the condition to add number in the "INSERT STUDENT {0}" what if the users want to insert more than 10 student name.

here's is what i have started:

List<string> studentName = new List<string>();

        string insertStudentOne = "INSERT STUDENT 1:";
        string insertStudentTwo = "INSERT STUDENT 2:";
        string listOfStudent = "List of Student:";

        string studentOne;
        string studentTwo;

        Console.Write(insertStudentOne);
        studentOne = Console.ReadLine();
        studentName.Add(studentOne);

        Console.Write(insertStudentTwo);
        studentTwo = Console.ReadLine();
        studentName.Add(studentTwo);

        string selectAnswer = "DO YOU WANT TO CONTINUE ADDING STUDENT (Y/N)?: ";
        string answerStr;
        Console.Write(selectAnswer);
        answerStr = Console.ReadLine();
        string name;

        if (answerStr.Equals("Y"))
        {
            {
                int addedStudent = 2;
                int sum;
                sum = addedStudent + 1;
                Console.Write("INSERTE STUDENT {0}: ", sum);
                name = Console.ReadLine();
            }

            Console.Write("DO YOU WANT TO CONTINUE ADDING STUDENT(Y / N) ?: ");
             string addedStudent1= Console.ReadLine();
            studentName.Add(addedStudent1);

            if (addedStudent1.Equals("N"))
            {
                Console.WriteLine(listOfStudent);
                Console.WriteLine(studentOne);
                Console.WriteLine(studentTwo);
                Console.WriteLine(name);

            }
            Console.ReadKey(); 

The sample program output should be like this:

INSERT STUDENT 1: JAMES INSERT STUDENT 2: JOHN DO YOU WANT TO CONTINUE ADDING STUDENT (Y/N)?:Y INSERT STUDENT 3: MICHAEL DO YOU WANT TO CONTINUE ADDING STUDENT (Y/N)?:N LIST OF STUDENTS: JAMES JOHN MICHAEL

INSERT STUDENT 1: JAMES INSERT STUDENT 2: JOHN DO YOU WANT TO CONTINUE ADDING STUDENT (Y/N)?:N LIST OF STUDENTS: JAMES JOHN

INSERT STUDENT 1: JAMES INSERT STUDENT 2: JOHN DO YOU WANT TO CONTINUE ADDING STUDENT (Y/N)?:Y INSERT STUDENT 3: MICHAEL DO YOU WANT TO CONTINUE ADDING STUDENT (Y/N)?:Y INSERT STUDENT 4: WILLIAM DO YOU WANT TO CONTINUE ADDING STUDENT (Y/N)?:Y INSERT STUDENT 5: JORDAN DO YOU WANT TO CONTINUE ADDING STUDENT (Y/N)?:N LIST OF STUDENTS: JAMES JOHN MICHAEL WILLIAM JORDAN

As you can see the "INSERT STUDENT {0} "added +1 after the "INSERT STUDENT 2" and everytime the answer is "Y" but if i Input the Letter "N" it will display all the inserted name Like sample program#2 if Input "N" after Student 2 it will display the student quickly and will always ask me if i want to cointinue adding student.However, if i alsways insert "Y" it will not stop to print the student inifinitely until i inserted "N". Some of my friends suggest that use foreach or LINQ to answer this. I'm just beginner in programming i want to learn programming deeper even i did not finish my college. i hope someone knows how to address my question. thanks in advance.



Solution 1:[1]

First of all check out arrays or lists which can store many variables. Otherwise you may need to create hundreds of student variables.

You may use a do-while loop to achieve the problem.

string answerStr = "";
do {
    string selectAnswer = "DO YOU WANT TO CONTINUE ADDING STUDENT (Y/N)?: ";
    Console.Write(selectAnswer);
    answerStr = Console.ReadLine();
    // get the user input
    // add the new user to a list or an array
    // then repeat the steps until the user gives an input other then "Y".
} while(answerStr == "Y");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bilal Bozkurt