Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The array is set up using:

C++
class Roster {
public:
	void add(string studentID, string firstName, string lastName, string email, int age, int daysInCourse1, int daysInCourse2, int daysInCourse3, Degree degreeProgram);
	void printAll();
	void printByDegreeProgram(int degreeProgram);
	void printDaysInCourse(string studentID);
	void printInvalidEmails();
	void remove(string studentID);
	~Roster();
	std::map<string, shared_ptr<Student> > classRosterArray;
};



void Roster::printAll()
{
    for (int i = 0; i < sizeof(classRosterArray) / sizeof(classRosterArray[i]); i++) {
        classRosterArray[i]->print();
    }
}

void Roster::printByDegreeProgram(int degreeProgram)
{
    for (int i = 0; i < sizeof(classRosterArray) / sizeof(classRosterArray[i]); i++) {
        if (classRosterArray[i]->fetchDegreeProgram() == degreeProgram) {
            classRosterArray[i]->print();
        }
    }
}

void Roster::printDaysInCourse(string studentID)
{
    float avg = 0;
    int max = 3;
    for (int i = 0; i < sizeof(classRosterArray) / sizeof(classRosterArray[i]); i++) {
        if (classRosterArray[i] != nullptr && classRosterArray[i]->fetchStudentId() == studentID) {
            int *daysInCourse = classRosterArray[i]->fetchDaysInCourse();
            for (int x = 0; x < max; x++) {
                avg += daysInCourse[x];
            }

            cout << "Student " << classRosterArray[i]->fetchStudentId() << "'s average number of days in each course is." << (avg / max) << "\n";
            break;
        }
    }
}

void Roster::printInvalidEmails()
{
    for (int i = 0; i < sizeof(classRosterArray) / sizeof(classRosterArray[i]); i++) {
        string email = classRosterArray[email]->fetchEmail();
        bool isValid = false;

        size_t found = email.find("@");
        if (found != string::npos) {
            found = email.find(".");
            if (found != string::npos) {
                found = email.find(" ");
                if (found == string::npos) {
                    isValid = true;
                }
            }
        }

        if (!isValid) {
            cout << email << " is not a valid email address \n";
        }
    }
}


What I have tried:

This was originally set up as an array initialized with nullptrs that does not work in C++ as it turns out for direct comparison loops. So on the advice of someone, I switched to the map which fixed one of my problems but broke the classarray loops later on and I am not sure exactly how to rewrite these to compensate.


Any help you guys can provide would be greatly appreciated
Posted
Updated 28-Nov-18 16:31pm
v2
Comments
jeron1 28-Nov-18 16:55pm    
something like?

for (int i = 0; i < classRosterArray.size(); i++)
{...
}

or try iterators, something like,

for (std::map::iterator it = classRosterArray.begin(); it != classRosterArray.end(); ++it)
{
...
}

1 solution

Something like this:
C++
void Roster::printDaysInCourse(string studentID)
{
    float avg = 0;
    int max = 3;
    for (auto it : classRosterArray) {
      int *daysInCourse = it->second->fetchDaysInCourse();
      for (int x = 0; x < max; x++) {
         avg += daysInCourse[x];
      }
      cout << "Student " << it->second->fetchStudentId() << "'s average number of days in each course is." << (avg / max) << "\n";
   }
}


But you have a design problem in that you can't have two students with the same name in your roster. If you insist on a map, put the student ID as the key, instead of the value (
std::map<shared_ptr<Student>, string> classRosterArray;
). That overcomes the problem. But I'd recommend using a vector of shared_ptrs, or unique_ptrs, instead of an array or a map, because you don't have to use the 'first' and 'second' nomenclature. You can also go back to something closer to your original design and overcome the lack of duplicate names issue. You will have to learn how to use vectors, though, and revamp your Roster class to add and remove from the vector properly.
 
Share this answer
 
v2

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