Control Class: Appending Object to Specific Array Based On User's Input

So I have a part of a program below that manages a collection of schools within a school board. Each school has a number of dynamically allocated students with their info such as their name and gender. Each school is assigned a specific ID number.

Additionally, the control class allows the user to create a new student and add it to a certain school based on the ID they enter.

So for example, here is what one user's input might look like:

Enter the data below:

School ID: 1001

Name: Emma Doyle

Gender: Female

So since the user entered '1001', it should create and add a female student named Emma to an already existing school with an ID of 1001 (which in this case would be the schoolTwo object because the next ID increments by 1 as shown in School.cc). Say if the ID's only range from 1-10, and the user enters an ID that doesn't exist like "1002", then nothing should happen and no student should be added.

So if I were to print everything in schoolTwo, it should print Emma Doyle and Johnny Doe.

What I'm having trouble with is accessing permissions from other classes and going about matching the entered school ID by the user to the existing school ID in the school district. I'm having trouble getting past restrictions and adding student objects to existing schools without having to create new ones under the launch() function when all the schools have already been created under the initStudents() function (shown as schoolOne and schoolTwo). Any help would be appreciated.

My attempt under Control.cc are the lines:

1
2
3
4
5
6
7

      Student* student = new Student(name, gender);
      if(fid == schoolDistrict.getNextId()){
            schoolDistrict.add(fid,student);

      }



but I know this won't work because everything is out of scope when I try to compile it and I think my logic is also wrong.

Control.cc:

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

#include <iostream>
using namespace std;
#include <string>
#include "Control.h"
#include "Student.h"
#include "Person.h"
#include "School.h"

void Control::launch()
{
  initAnimals();
  int    choice, fid;
  string name, gender;  


  while (1) {
    view.showMenu(choice);
    if (choice == 0)
      break;

    if (choice == 1) { 

          view.printStr("School ID: ");
          view.readInt(fid);
          view.printStr("Name: ");
          view.readStr(name);
          view.printStr("Gender: ");
          view.readInt(gender);

          Student* student = new Student(name, gender);
          if(fid == schoolDistrict.getNextId()){
                schoolDistrict.add(fid,student);

          }
    }
  }
}

void Control::initStudents(){

    School* schoolOne;
    School* schoolTwo;

    schoolOne = new School();
    schoolTwo = new School();

    schoolDistrict.add(schoolOne);
    schoolDistrict.add(schoolTwo);

    Student* studentOne;
    studentOne = new Student("Johnny Doe", "Male");
    schoolTwo->add(student);
}


Control.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

#ifndef CONTROL_H
#define CONTROL_H
#include "View.h"
#include "SchoolDistrict.h"
class Control
{
  public:
    void launch();

  private:
    View view;
    SchoolDistrict schoolDistrict;
    void initStudents();

};


School.cc:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

#include <iostream>
using namespace std;
#include <string>
#include "School.h"

int School::nextId = 1000;

School::School(){
  id = nextId;
  ++nextId;
}

int School::getNextId() { 
    return nextId; 
}

void School::add(Student* s){
    studentsList.add(s);
}


SchoolDistrict.cc:

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

#include <iostream>
#include <iomanip>
using namespace std;
#include <string>

#include "SchoolDistrict.h"


SchoolDistrict::SchoolDistrict(){
    currentNumSchools = 0;
}

SchoolDistrict::~SchoolDistrict()
{ 
 //code
}

void SchoolDistrict::add(School* s){ // adds the given school s to the back (the end) of the school array
    if(currentNumSchools != MAX_SCHOOLS){
        for(int i = 0; i < currentNumSchools; ++i){
            schools[i] = s;
            ++currentNumSchools;
        }
    }else{
        cout << "Maximum number of schools reached. " << endl;
    }
}

void SchoolDistrict::add(int id, Student* s){  
//adds the given student s to the school with the unique identifier matching the parameter id
    for(int i = 0; i  < currentNumSchools; ++i){
        if(schools[i]->getNextId() == id){
            schools[i]->add(s);
        }
    }
}
I should also mention that having a getNextId() function is required, so I'm unfortunately stuck with my confusing approach. :(
You cannot use getNextId() when you want to add a student to a certain school. Instead you need to find that particular school by its actual id.

In SchoolDistrict.cc line 33 you need the actual id (maybe a function like getId() instead of getNextId()) of the school. That's it. Remove line 32 in Control.cc.

Add a function like:
1
2
3
int School::getId() { 
    return id; 
}


1
2
3
4
5
6
7
8
void SchoolDistrict::add(int id, Student* s){  
//adds the given student s to the school with the unique identifier matching the parameter id
    for(int i = 0; i  < currentNumSchools; ++i){
        if(schools[i]->getId() == id){ // Note: getId()
            schools[i]->add(s);
        }
    }
}
Topic archived. No new replies allowed.