Click here to Skip to main content
15,882,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
/*
The entire program works correctly except for the last function: removeStudent.
When I remove a student from a particular course, there is no issue - until I want to print out a listing of student currently enrolled in the course that the student was removed from. I *think* the issue is the variable numberofStudents is getting the wrong value. I have no idea why this is happening. The reason I am only storing the student ID in each course, not the entire student object is because names of students can be modified and it doesn't make sense to duplicate the data and be required to update it twice.
Issue is: Adding student, Adding course, removing student, printing students in class that the student was previously enrolled in.
Below is my code. Only three classes. Nothing wrong with Student or Main.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApp1
{
public class Course
{
public string courseName { get; set; }
public string teacherFirst { get; set; }
public string teacherLast { get; set; }
// used as the primary key for the course.
public static int courseCount = 0;
// used to store each course.
public static Dictionary<int, Course> courses = new Dictionary<int, Course>();
// used to store the id of each student in a course.
List
<int>
studentsInCourse = new List
<int>
();
// constructor for creating the course object.
public Course(string name, string first, string last, List
<int>
newStudentList)
{
courseName = name;
teacherFirst = first;
teacherLast = last;
studentsInCourse = newStudentList;
}
// method that creates the course.
public static void addCourse()
{
courseCount++;
Console.Write("\nPlease enter the name of the course: "); // grabbing input from user.
string name = Console.ReadLine();
Console.Write("Please enter the teacher's first name: "); // grabbing input from user.
string first = Console.ReadLine();
Console.Write("Please enter the teacher's last name: "); // grabbing input from user.
string last = Console.ReadLine();
List
<int>
studentsInCourse = new List
<int>
();
Course added = new Course(name, first, last, studentsInCourse);
courses.Add(courseCount, added);
}
// prints a listing of all courses currently available.
public static void printCourse()
{
foreach (KeyValuePair<int, Course> pair in courses)
{
Console.WriteLine("\nCourse ID: {0}", pair.Key);
Console.WriteLine("Course Name: {0}", pair.Value.courseName);
Console.WriteLine("Teacher's Name: {0} {1}", pair.Value.teacherFirst, pair.Value.teacherLast);
}
}
// adding student to a specific course.
public static void addStudentToCourse()
{
Console.Write("Please enter the student's ID: ");
string studentInput = Console.ReadLine();
int studentID = Convert.ToInt32(studentInput);
Console.Write("Please enter the course ID: ");
string courseInput = Console.ReadLine();
int courseID = Convert.ToInt32(courseInput);
courses[courseID].studentsInCourse.Add(studentID);
}
// print all students assigned to a particular course.
public static void printStudentsInCourse()
{
Console.Write("Please enter the course ID: ");
string courseInput = Console.ReadLine();
int courseID = Convert.ToInt32(courseInput);
int numberofStudents = courses[courseID].studentsInCourse.Count();
Console.WriteLine("\nCourse ID: {0}", courses[courseID].courseName);
Console.WriteLine("\nThe number of students currently enrolled in this course are: {0}\n", numberofStudents);
for (int i = 0; i < numberofStudents; i++)
{
string x = courses[courseID].studentsInCourse[i].ToString();
int addID = Convert.ToInt32(x);
Console.WriteLine("Student ID: {0} \nFull Name: {1} {2}\n", x, Student.current[addID].FirstName, Student.current[addID].LastName);
}
}
public static void removeStudent(int studentID)
{
// grabbing number of courses currently populated.
int numberofCourses = courses.Count;
// going through each course removing student if necessary.
for (int i = 1; i < numberofCourses; i++)
{
// grabbing number of students in specific course.
int numberofStudents = courses[i].studentsInCourse.Count();
// iterating through looking for student id to remove.
for (int z = 0; z < numberofStudents; z++)
{
if(courses[i].studentsInCourse[z] == studentID)
{
courses.Remove(courses[i].studentsInCourse[z]);
}
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApp1
{
public class Student
{
public string FirstName { get; set; }
public string LastName { get; set; }
// used as the primary key for the student.
public static int student_ID = 0;
public Student(string first, string last)
{
FirstName = first;
LastName = last;
}
public static Dictionary<int, Student> current = new Dictionary<int, Student>
();
// used to print all students in the system.
public static void printStudents()
{
if (current.Count == 0)
{
Console.WriteLine("There are currently no students that have been
added to the service.");
}
else
{
foreach (KeyValuePair<int, Student> pair in current)
{
Console.WriteLine("\nStudent ID: {0}", pair.Key);
Console.WriteLine("First Name: {0}", pair.Value.FirstName);
Console.WriteLine("Last Name: {0}", pair.Value.LastName);
}
}
}
// used to see if a student exist with the student id entered from user.
public static bool findStudentID(int studentID)
{
bool value = false;
if (current.ContainsKey(studentID))
{
Console.WriteLine("\nA student was found with that ID Number.\n");
value = true;
}
else
{
Console.WriteLine("\nA student was *not* found with that ID Number.");
Console.WriteLine("No edits will be made.");
}
return value;
}
// used to edit first name of a particular student.
public static void editFirstName(int studentID, string newFirstName)
{
foreach (KeyValuePair<int, Student> pair in current)
{
if (pair.Key == studentID)
{
pair.Value.FirstName = newFirstName;
}
}
}
// used to edit the last name of a particular student.
public static void editLastName(int studentID, string newLastName)
{
foreach (KeyValuePair<int, Student> pair in current)
{
if (pair.Key == studentID)
{
pair.Value.LastName = newLastName;
}
}
}
// remove student from the student dictionary.
public static void removeStudent(int studentID)
{
current.Remove(studentID);
}
}
}
using System;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string input = null;
while (input != "10")
{
printMenu();
input = Console.ReadLine();
switch (input)
{
// add a new student.
case "1":
{
addStudent();
break;
}
// print out all student's names.
case "2":
{
Student.printStudents();
break;
}
// Edit student's first name.
case "3":
{
Console.Write("Please enter the student ID: ");
string userInput = Console.ReadLine();
int studentID = Convert.ToInt32(userInput);
bool value = Student.findStudentID(studentID);
if (value == true)
{
Console.Write("Please enter the new first Name: ");
string newFirstName = Console.ReadLine();
Student.editFirstName(studentID, newFirstName);
}
break;
}
// Edit student's last name.
case "4":
{
Console.Write("Please enter the student ID: ");
string userInput = Console.ReadLine();
int studentID = Convert.ToInt32(userInput);
bool value = Student.findStudentID(studentID);
if (value == true)
{
Console.Write("Please enter the new last Name: ");
string newLastName = Console.ReadLine();
Student.editLastName(studentID, newLastName);
}
break;
}
// remove student from list and all courses the student is
assigned to.
case "5":
{
Console.Write("Please enter the student ID: ");
string userInput = Console.ReadLine();
int studentID = Convert.ToInt32(userInput);
Student.removeStudent(studentID);
Course.removeStudent(studentID);
break;
}
// add course.
case "6":
{
Course.addCourse();
break;
}
// listing of all current courses available.
case "7":
{
Course.printCourse();
break;
}
// add student to a course.
case "8":
{
Course.addStudentToCourse();
break;
}
// print all students in a particular course.
case "9":
{
Course.printStudentsInCourse();
break;
}
// exit the program.
case "10":
{
Console.WriteLine("Closing program.");
Console.ReadKey();
break;
}
// an input was entered that was not valid to the menu.
default:
{
Console.WriteLine("*The input entered was not valid.*");
//printMenu();
break;
}
}
}
}
// a listing of all options available in the program.
public static void printMenu()
{
Console.WriteLine("\nMenu");
Console.WriteLine("To add a student enter 1.");
Console.WriteLine("To receive listing of all students enter 2.");
Console.WriteLine("Edit a student's first name enter 3.");
Console.WriteLine("Edit a student's last name enter 4.");
Console.WriteLine("To remove a student enter 5.");
Console.WriteLine("To add a course enter 6.");
Console.WriteLine("To get a listing of all current courses enter
7.");
Console.WriteLine("To add a student to a course enter 8.");
Console.WriteLine("To get a listing of all students in a course enter
9.");
Console.WriteLine("To quit the program enter 10.\n");
Console.Write("Please enter selection: ");
}
public static void addStudent()
{
Student.student_ID++;
Console.Write("\nPlease enter student's first name: "); // grabbing
input from user.
string firstName = Console.ReadLine();
Console.Write("Please enter student's last name: "); // grabbing
input from user.
string lastName = Console.ReadLine();
Student added = new Student(firstName, lastName);
Student.current.Add(Student.student_ID, added);
}
}
}

What I have tried:

I've tried finding out why the variable numberofStudents is giving the wrong value for a particular class, after a student has been removed but no luck.

I offered a free coffee to whoever helped me yesterday.
However Gerry Schmitz did not want to take me up on the offer.
10 dollar starbucks gift card for whoever helps me today.
I'm not here for handouts.
Posted
Updated 10-Jul-18 19:25pm

Quote:
I *think* the issue is the variable numberofStudents is getting the wrong value.

Don't think, use the debugger to ensure!

Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

The downside of this solution:
- It is a DIY, you are the one tracking the problem and finding its roots, which lead to the solution.
The upside of this solution:
- It is also a great learning tool because it show you reality and you can see which expectation match reality.

secondary effects
- Your will be proud of finding bugs yourself.
- Your learning skills will improve.

You should find pretty quickly what is wrong.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
Debugging C# Code in Visual Studio - YouTube[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
// iterating through looking for student id to remove.
for ( int z = 0; z < numberofStudents; z++ ) {
   if ( courses[ i ].studentsInCourse[ z ] == studentID ) {

      // courses.Remove( courses[ i ].studentsInCourse[ z ] );

      courses[ i ].studentsInCourse.Remove( studentID );
      break;
   }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900