So I'm trying to code using a class that takes in StudentID, name, and classification(freshman-senior) up to seven students. I think I can figure out the drop() function. I'm having trouble printing out the array(roster()) after it is all input. I marked where I think the problem is. here is an example...
they input...
49584 John Freshman {enter}
69976 Alex senior {enter}
.
.
.
up to 7 students
I believe it's all stored in an array until I go to print out the array and all I get are the seven studentID's.
There are currently a few problems with your code. On both lines 43 and 59 you are creating 7 completely new Student objects Student stu[7]; ignoring any of the ones that have been entered before.
You will want to erase both of those and put Student stu[7]; at the beginning of your main function, before the while loop starts so that it is always referring to those same 7 student objects.
You should also replace for (int i =0; i<7; i++) on line 60 with for (int i =0; i<Count; i++) so that it only prints out the students that have actually been filled in.
Thanks James that worked out perfectly. I didn't realize that were referring to those same 7 students every time. I got just one more question if you don't mind helping me out. So I was working on the drop function in my code and I can't quite get it right. After the user enters the students they should be allowed to enter by Student ID to delete a student from the roster then the roster should shift over to the left after it has been deleted...
example...
they enter...
54435 John freshman {enter}
54090 Erin senior {enter}
43323 Alex senior {enter}
34943 Linda junior {enter}
Do you have to use arrays for this project? It would be much easier in the long run if you switched to vectors or lists as they are much better at adding/removing elements at run time. (If you are going to stay with arrays you will prob have to dynamically allocate memory for it to remove elements, which will complicate things).
I can use list or arrays. I only used an array b/c thats the only way I thought I could do it. If I use a list will I have to change my whole code or just the arrays? And how would I go about doing that?
list<Student> stu;
while (a != "quit")
{
cout << "class>";
cin >> a;
// Could use stu.size() here if you want to limit how many can be entered
if (a == "enroll")
{
Student Temp; // Temporary student object to insert data into
Temp.SID(); // Get data from the user
stu.push_back(Temp); // Insert object into the list
}
elseif (a == "drop")
{
int ID;
cout << "Enter ID to remove" << endl;
cin >> ID;
// Loops through the entire student list with iterators
for(list<Student>::iterator i = stu.begin(); i != stu.end(); i++)
{
// Looks to see if any ID matches the one the user entered
if(i->StudentID == ID)
{
stu.erase(i); // Erases that object from the list
break; // Stops looping
}
}
}
That would be the general idea anyway (hopefully you can figure out how to do the roster from that, as it will be good practice).
The good think about this approach is you can remove any element from the list without causing any trouble to anything else. If you don't understand some of the code there are several pages explaining lists/iterators around google.