classes and array

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.

thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
using namespace std;

class Student
{
    public:
        Student() {};
        ~Student() {};

        void display()
        {
        cout << StudentID << " " << Name << " " << Classification << endl;
        }

        void SID()
        {
        cout << "class>Enroll Student:";
        cin >> StudentID >> Name >> Classification;
        }

    private:
        int StudentID;
        string Name;
        string Classification;
};



string a = " ";
int count = 0;
int main()
{

while (a != "exit")
{
    cout << "class>";
    cin >> a;

    if (a == "enroll")
    {
        if (count < 7)
        {
                Student stu[7];
                stu[count].SID();
                stu[count].display();
                count++;
        }
        else
           cout << "ERROR! Roster is full." << endl;

    }
    else if (a == "drop")
    {
        Student stu;

    }
    else if (a == "roster")
    {
        Student stu[7];
        for (int i =0; i<7; i++)
        //***I think this is where my problem is****
        stu[i].display();
    }
    else
        cout << "ERROR! please enter another word" << endl;
}
return 0;
}
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.
Last edited on
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}

then they want to erase and all they enter
54090

and I want it to do this...

54435 54090 43323 34943
(they erase 54090)..
54435 43323 34943


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <iostream>
using namespace std;

class Student
{
    public:
        Student() {};
        ~Student() {};

        void display()
        {
        cout << StudentID << " " << Name << " " << Classification << endl;
        }

        void SID()
        {
        cout << "class>Enroll Student:" << endl;
        cin >> StudentID >> Name >> Classification;
        }

    private:
        int StudentID;
        string Name;
        string Classification;
};



string a = " ";
int count = 0;
int main()
{
Student stu[7];
while (a != "quit")
{
    cout << "class>";
    cin >> a;

    if (a == "enroll")
    {
        if (count < 7)
        {
                stu[count].SID();
                stu[count].display();
                count++;
        }
        else
           cout << "ERROR! Roster is full." << endl;

    }
    else if (a == "drop")
    {  
     //****This is where I'm having a problem****
        int matchID =0;
        cout << "class>Enter ID: " << endl;
        cin >> matchID;
        for (int i = count; i < stu[count]; i++)
        stu[count] = stu[count+1];
        stu[stu[count-1] = 0;

    }
    else if (a == "roster")
    {
        for (int i =0; i<count; i++)
        stu[i].display();
    }
    else
        cout << "ERROR! please enter another word" << endl;
}
return 0;
}




I can't figure out where to put the matchID on line 55 for my for loop. I feel like this is completely wrong.
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).

http://www.cplusplus.com/reference/stl/vector/
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?
Only your array code needs to be changed. Here is an example of how you could do it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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
     }

    else if (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.
Topic archived. No new replies allowed.