Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4246842
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:01:43+00:00 2026-05-21T04:01:43+00:00

I am having some trouble as follows: When i add a new user, it

  • 0

I am having some trouble as follows: When i add a new user, it works fine but when I add the second user, it forgets the first one. It keeps forgetting the preceding user added before it. Here is the code:

    static List<Students> stud = new List<Students>();
    static Courses regcor = new Courses(0,"");
    static void Main(string[] args)
    {
        Students regstud = new Students("", "", "", 0);
        string idselec = "";
        string selec = "";
        do
        {


            Console.WriteLine("1.Register new student.");
            Console.WriteLine("2.Add course.");
            Console.WriteLine("3.All information.");
            Console.WriteLine("4.Exit.");
            selec = Console.ReadLine();
            if (selec == "1")
            {
                do
                {
                    Console.Clear();
                    Console.WriteLine("Enter ID");
                    idselec = Console.ReadLine();
                    if (checkid(idselec))
                    {
                        Console.WriteLine("Id already excsists");
                        Console.ReadLine();
                    }

                }
                while (checkid(idselec));
                regstud.ID = idselec;
                Console.WriteLine("Enter Name");
                regstud.name = Console.ReadLine();
                Console.WriteLine("Enter Surname");
                regstud.surname = Console.ReadLine();
                Console.WriteLine("Enter Age");
                regstud.age = Convert.ToInt32(Console.ReadLine());
                stud.Add(regstud);
            }
            else if (selec == "2")
            {
                Console.WriteLine("Enter ID");
                idselec = Console.ReadLine();
                check(idselec);

            }
            else if (selec == "3") 
            {
                Console.Clear();
                writeall();
            }

        }
        while (selec != "4");


    }
    static bool checkid(string id)
    {

        return stud.Any(u => u.ID == id);
    }

    static void check(string ID)
    {
        int i = 0;
        bool found = false;
        do
        {
            if (stud[i].ID == ID )
            {

                Console.WriteLine("Hello " + stud[i].name+" "+ stud[i].surname) ;
                Console.WriteLine("Enter code");
                int code =Convert.ToInt32( Console.ReadLine());
                Console.WriteLine("Enter Name");
                string name = Console.ReadLine();

                regcor.CourID = code;
                regcor.courname = name;
                stud[i].cour(regcor);

                found = true;


            }

            i++;
        }
        while ((i < stud.Count) && !(found));




    }
    static void writeall()
    {
        int i = 0, y=0, sub=0,sub3=0;
        string sub2="";
        do
        {
            Console.WriteLine(stud[i].ID);
            Console.WriteLine(stud[i].name);
            Console.WriteLine(stud[i].surname);
            Console.WriteLine(stud[i].age);
            sub3 = stud[i].cour3();
            do
            {
                sub = stud[i].cour1(y);
                sub2 = stud[i].cour2(y);
                Console.WriteLine(sub);
                Console.WriteLine(sub2);

                y++;


            }
            while (y < sub3);
            i++;
        }
        while (i < stud.Count);
    }
}

}

This is the class Courses:

    public int CourID = 0;
    public string courname = "";
    public Courses(int corID, string corname)
    {
        this.CourID = corID;
        this.courname = corname;

    }
}

}

This is the class Students;

    public int age = 0;
    public string name = "", surname = "", ID = "";
    List<Courses> cours = new List<Courses>();
    public Students(string name, string surname,string ID, int age)
    {
        this.ID = ID;
        this.surname = surname;
        this.age = age;
        this.name = name;
    }
    public void cour(Courses c)
    {
        cours.Add(c);
    }
    public int cour1(int i)
    {
       return cours[i].CourID;

    }
    public string cour2(int i)
    {
      return  cours[i].courname;
    }
    public int cour3()
    {
        return cours.Count;
    }

}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T04:01:44+00:00Added an answer on May 21, 2026 at 4:01 am

    You are currently only creating one student, then overwrite his properties

    Move this:

    Students regstud = new Students("", "", "", 0);
    

    into your do loop where you actually create a new student:

    if (selec == "1")
    {
       Students regstud = new Students("", "", "", 0);
       ...
    }
    

    Even better would be only creating the Student object once you have all of his properties ready:

    if (selec == "1")
    {
       ...
       Console.WriteLine("Enter Name");
       string name = Console.ReadLine();
       Console.WriteLine("Enter Surname");
       string surname = Console.ReadLine();
       Console.WriteLine("Enter Age");
       int age = Convert.ToInt32(Console.ReadLine());
    
       Students regstud = new Students(name, surename, id, age);
       ...
    }
    

    Besides this your code looks like of a combination of Stone Age and Futurama to me: You use archaic loop constructs with separate index variables on the one hand, and then generics and LINQ on the other. Try to stick with higher abstractions as possible – most loop variables you use can be avoided, if you use ForEach() etc – this will make your code much cleaner and easier to read.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to hibernate and I am having some trouble with the named
I'm playing around with GHCi for the first time, and I'm having some trouble
I'm having some trouble sending along more than one variable to the view. my
I'm new to PHPUnit, and I'm having some trouble with unit testing HTML output.
Basically I am trying to show hide divs and links, but having some trouble.
I am having some trouble structuring my ReportFrame.SourceURL. My code as follows in my
I am having some trouble here, my json data is outputting as follows: {date:[{day_w:Tuesday,day_n:28,month:Dec}],subscriptions:[{subscribe:example1},{subscribe:example2},{subscribe:example3}]}
I am having some trouble getting a List of one of my classes that
I'm having some trouble with displaying numbers in apex, but only when i fill
I a implementing single table inheritance but am having some trouble with the routing.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.