Frank Moricz


Web Portfolio and Art Gallery

Magic made with caffeine and a keyboard.

C++ Code Example - Student Roster

These are code samples from a final project used to complete a C++ course.  While the program functionality was simple, the instructions were designed to force many advanced methods to complete the objectives.

As this was (and may still a currently be) an assignment, I removed some key functionality and have not posted the complete source.

 

Student.h:

#ifndef STUDENT_H
#define STUDENT_H

#include "degree.h"
#include 
using namespace std;

class Student {
public:
	//Accessors
	string GetStudentID();
	string GetStudentFirstName();
	string GetStudentLastName();
	string GetEmailAddress();
	int GetStudentAge();
	int GetDaysToComplete(int classNum);
	DegreeType GetDegreeType();

	
	//Mutators
	void SetStudentID(string idNum);
	void SetStudentFirstName(string name);
	void SetStudentLastName(string name);
	void SetStudentEmail(string email);
	void SetStudentAge(int ageNumber);
	void SetDaysToComplete(int days, int classNumber);
	void SetDegreeType(DegreeType type);
	//Constructor
	Student();
	//Destructor
	~Student();
	//Other Functions
	virtual void Print() {};
	virtual DegreeType GetDegreeProgram() { return SOFTWARE; };

	//Variables 
	string studentID;
	string firstName;
	string lastName;
	string emailAddress;
	int age;
	int daysToComplete[3];
	DegreeType degreeType;

};

#endif // !STUDENT_H

Roster.h :

#include 
#include "roster.h"
using namespace std;

Roster::Roster() {

}
Roster::~Roster() {
	delete classRosterArray[0];
	delete classRosterArray[1];
	delete classRosterArray[2];
	delete classRosterArray[3];
	cout << "Roster Class Destroyed" << endl;
}

void Roster::SetMyWGUData() {
		wguData = "Scripting and Programming _ Applications _ C867, C++, WGU Student Id#001201811, Frank Moricz";
}

void Roster::PrintIndividualStudentData(Student stu) {
	stu.Print();
}

void Roster::Remove(string studentId) {
	//Remove student from roster, kick back error message if student not found to remove
	bool idFound = false;
	int arrayLocation;
	for (int x = 0; x < 5; x++) {
		if (classRosterArray[x]->GetStudentID() == studentId) {
			idFound = true;
			arrayLocation = x;
			break;
		}
	}
	if (idFound) {
		cout << "Student ID located - Removing from roster" << endl;
		//classRosterArray[arrayLocation]->SetStudentID("XX");

		while (arrayLocation < 4) {
			classRosterArray[arrayLocation] = classRosterArray[arrayLocation + 1];
			arrayLocation++;			
		}
	}
	else {
		cout << "Unable to locate chosen Student ID.  Aborting removal" << endl;
	}

}

void Roster::PrintDaysInCourse(string studentID) {
	for (int x = 0; x < 5; x++) {
		if (classRosterArray[x]->GetStudentID() == studentID) {
			int class1 = classRosterArray[x]->GetDaysToComplete(0);
			int class2 = classRosterArray[x]->GetDaysToComplete(1);
			int class3 = classRosterArray[x]->GetDaysToComplete(2);

			double average = (class1 + class2 + class3) / 3;
			cout << "Average completion days for " << studentID << ": " << average << endl;
			break;
		}
	}
}

void Roster::PrintByDegreeProgram(DegreeType dType) {
	for (int x = 0; x < 5; x++) {
		if (classRosterArray[x]->GetDegreeType() == dType) {
			cout << classRosterArray[x]->GetStudentID() << "t"
				<< classRosterArray[x]->GetStudentFirstName() << "t"
				<< classRosterArray[x]->GetStudentLastName() << "t" << endl;
		}
	}
}

void Roster::ParseStudentData(const string studentData[]) {
	//Parse data blob into neater string array using commas to determine word seperation
	//Create temporary array to remove commas
	string* tempArray[45];
	int rosterPosition = 0;
	int delimiterPosition = 0;
	for (int x = 0; x < 5; x++) {
		string currentString = studentData[x];
		string delimiter = ",";
		for (int y = 0; y < 9; y++) {
			string* s = nullptr;
			s = new string;
			*s = currentString.substr(0, currentString.find(delimiter));
			//s = memory address for data piece, *s is dereferenced data
			tempArray[rosterPosition] = s;
			currentString.erase(0, currentString.find(delimiter) + 1);
			rosterPosition++;
		}
	}
	//cout << "Data Parsed into temporary array" << endl;

	//Create Student Objects
	classRosterArray[0] = new SecurityStudent();
	classRosterArray[1] = new NetworkStudent();
	classRosterArray[2] = new SoftwareStudent();
	classRosterArray[3] = new SecurityStudent();
	classRosterArray[4] = new SoftwareStudent();
	//cout << "objects created" << endl;

	//Set Degree Programs
	classRosterArray[0]->SetDegreeType(classRosterArray[0]->GetDegreeProgram());
	classRosterArray[1]->SetDegreeType(classRosterArray[1]->GetDegreeProgram());
	classRosterArray[2]->SetDegreeType(classRosterArray[2]->GetDegreeProgram());
	classRosterArray[3]->SetDegreeType(classRosterArray[3]->GetDegreeProgram());
	classRosterArray[4]->SetDegreeType(classRosterArray[4]->GetDegreeProgram());
	//cout << "Degrees set" << endl;

	//Loop through temp pointer array and populate all student data via pointers
	int arrayLoc = 0;
	for (int x = 0; x < 5; x++) {
		//PopulateStudent(x, arrayLoc); //Populate Vector

		Add(*tempArray[arrayLoc],
			*tempArray[arrayLoc + 1],
			*tempArray[arrayLoc + 2],
			*tempArray[arrayLoc + 3],
			stoi(*tempArray[arrayLoc + 4]),
			stoi(*tempArray[arrayLoc + 5]),
			stoi(*tempArray[arrayLoc + 6]),
			stoi(*tempArray[arrayLoc + 7]),
			x);
		arrayLoc += 9;
	}
	//cout << "Add function completed" << endl;

}

void Roster::Add(string studentID, string firstName, string lastName, string emailAddress, int age, int daysInCourse1, int daysInCourse2, int daysInCourse3, int rosterPosition) {
	classRosterArray[rosterPosition]->SetStudentID(studentID);
	classRosterArray[rosterPosition]->SetStudentFirstName(firstName);
	classRosterArray[rosterPosition]->SetStudentLastName(lastName);
	classRosterArray[rosterPosition]->SetStudentEmail(emailAddress);
	classRosterArray[rosterPosition]->SetStudentAge(age);
	classRosterArray[rosterPosition]->SetDaysToComplete(daysInCourse1, 0);
	classRosterArray[rosterPosition]->SetDaysToComplete(daysInCourse2, 1);
	classRosterArray[rosterPosition]->SetDaysToComplete(daysInCourse3, 2);
}

void Roster::PrintInvalidEmails() {
	//Loop through all student emails and output errors
	for (int x = 0; x < 5; x++) {
		string stuId = classRosterArray[x]->GetStudentID();
		string email = classRosterArray[x]->GetEmailAddress();
		//ensure there is both an @ and a period, and no spaces.
		int locAtSymbol = (email).find("@");
		int locPeriod = (email).find(".");
		int locSpace = (email).find(" ");

		bool emailValid = true;
		string reason;
		if (locAtSymbol == -1 || locPeriod == -1) {
			emailValid = false;
			reason = " Addresses must contain both an @ symbol and a '.' symbol.";
		}
		if (locSpace != -1) {
			emailValid = false;
			reason = " Spaces are not allowed in an email address.";
		}

		if (!emailValid) {
			cout << "Email address for student ID: " << stuId << " error. '" << email << "'  " << reason << endl;
		}
	}
}

void Roster::PrintAll() {
	//Print list of all students to screen

	for (int x = 0; x < 5; x++) {
		string stuID = classRosterArray[x]->GetStudentID();

		//Prvent printing duplicate entries
		bool printThis = true;
		if (x > 0) {
			if (classRosterArray[x - 1]->GetStudentID() == stuID) {
				printThis = false;
			}
		}


		if (printThis) {
			cout << stuID << "t";
			cout << "First Name: " << classRosterArray[x]->GetStudentFirstName() << "t";
			cout << "Last Name: " << classRosterArray[x]->GetStudentLastName() << "t";
			cout << "Age: " << classRosterArray[x]->GetStudentAge() << "t";
			cout << "Days in course: {" << classRosterArray[x]->GetDaysToComplete(0) << ", " << classRosterArray[x]->GetDaysToComplete(1) << ", " << classRosterArray[x]->GetDaysToComplete(2) << "}";
			cout << "Degree Program: ";
			if (classRosterArray[x]->GetDegreeType() == SOFTWARE) {
				cout << "Software" << "t";
			}
			else if (classRosterArray[x]->GetDegreeType() == NETWORKING) {
				cout << "Networking" << "t";
			}
			else {
				cout << "Security" << "t";
			}
			cout << endl;
		}
	}
}


void main()
{
	Roster classRoster;
	//WGU Student Data
	classRoster.SetMyWGUData();
	cout << classRoster.wguData << endl;
	cout << endl;

	//Table data from assignment for parsing
	const string studentData[] =
	{ "A1,John,Smith,John1989@gm ail.com,20,30,35,40,SECURITY",
	"A2,Suzan,Erickson,Erickson_1990@gmailcom,19,50,30,40,NETWORK",
	"A3,Jack,Napoli,The_lawyer99yahoo.com,19,20,40,33,SOFTWARE",
	"A4,Erin,Black,Erin.black@comcast.net,22,50,58,40,SECURITY",
	"A5,Frank,Moricz,fmoricz@wgu.edu,36,5,6,10,SOFTWARE" };

	//Create pointer array
	classRoster.ParseStudentData(studentData);
	//Create student object for each student in table

	//Print Student data
	cout << "Current Student Data: " << endl;
	classRoster.PrintAll();
	cout << endl;

	//Print Invalid Emails
	cout << "Checking Student Emails: ";
	classRoster.PrintInvalidEmails();
	
	
	//Print course averages for each student in table:
	cout << endl << "Course Average Completion Times: " << endl;
	classRoster.PrintDaysInCourse("A1");
	classRoster.PrintDaysInCourse("A2");
	classRoster.PrintDaysInCourse("A3");
	classRoster.PrintDaysInCourse("A4");
	classRoster.PrintDaysInCourse("A5");

	//List by degree type SOFTWARE
	cout << endl << "Lising students by degree type: SOFTWARE" << endl;
	classRoster.PrintByDegreeProgram(SOFTWARE);

	//Remove student A3, twice
	cout << endl << "Removing Student A3: ";
	classRoster.Remove("A3");
	cout << endl << "Removing Student A3: ";
	classRoster.Remove("A3");

	//Reprint student Data
	cout << "Current Student Data: " << endl;
	classRoster.PrintAll();
	cout << endl;

	//classRoster.classRosterArray[0]->Print();  //Virtual Print Functionality

}