Click here to Skip to main content
15,892,517 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
class Student {
public:
	//Accessor
	int fetchAge();
	int *fetchDaysInCourse();
	string fetchEmail();
	string fetchFirstName();
	string fetchLastName();
	string fetchStudentId();
	Degree fetchDegreeType();
	//Mutators
	void mutateAge(int);
	void mutateDaysInCourse(int[1]);
	void mutateEmail(string);
	void mutateFirstName(string);
	void mutateLastName(string);
	void mutateStudentId(string);
	void mutateDegreeType(Degree);
	
	//other handlers
	virtual void print();
	virtual Degree fetchDegreeProgram();
	//constructor
	Student(int age, int daysInCourse[3], string studentId, string email, string firstName, string lastName, Degree degreeType);
	~Student();

private:
	int Age, DaysInCourse[1];
	string Email, FirstName, LastName, StudentId;
	Degree DegreeType;

};

C++
using namespace std;

int main() {

	//personal information for start screen
	cout << "Course Title: Scripting and Programming - Applications - C867 \n";
	cout << "Language used: C++ \n";
	cout << "Student ID: 467185 \n";
	cout << "Written by: Gregory Young \n\n";

	Roster classRoster;

	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,Gregory,young,gyoun15@wgu.edu,29,30,20,33,40,SOFTWARE"
	};

	//fill the class roster array
	for (int i = 0; i < sizeof(studentData) / sizeof(studentData[i]); i++) {
		cout << studentData[i] << endl;
		string input = studentData[i];
		istringstream ss(input);
		string token;
		string tempHolder[9];

		int x = 0;
		while (x < 9 && getline(ss, token, ',')) {
			tempHolder[x] = token;
			x += 1;
		}

		Degree studentDegree;
		if (tempHolder[8] == "NETWORK") {
			studentDegree = NETWORKING;
		}
		else if (tempHolder[8] == "SECURITY") {
			studentDegree = SECURITY;
		}
		else if (tempHolder[8] == "SOFTWARE") {
			studentDegree = SOFTWARE;
		}
		else {
			studentDegree = NODEGREE;
		}

		classRoster.add(tempHolder[0], tempHolder[1], tempHolder[2], tempHolder[3], std::stoi(tempHolder[4]), std::stoi(tempHolder[5]), std::stoi(tempHolder[6]), std::stoi(tempHolder[7]), studentDegree);
	}

	classRoster.printAll();
	cout << "\n";
	classRoster.printInvalidEmails();
	cout << "\n";
	for (int i = 0; i < sizeof(classRoster.classRosterArray) / sizeof(classRoster.classRosterArray[i]); i++) {
		classRoster.printDaysInCourse(classRoster.classRosterArray[i]->fetchStudentId());
	}
	cout << "\n";
	classRoster.printByDegreeProgram(SOFTWARE);
	cout << "\n";
	classRoster.remove("A3");
	classRoster.remove("A3");

	return 0;
}

void Roster::add(string studentID, string firstName, string lastName, string email, int age, int daysInCourse1, int daysInCourse2, int daysInCourse3, Degree degreeProgram)
{
	int courseDays[3] = { daysInCourse1, daysInCourse2, daysInCourse3 };

	for (int i = 0; i < sizeof(classRosterArray) / sizeof(classRosterArray[i]); i++) {
		if (classRosterArray[i] == nullptr) {
			if (degreeProgram == NETWORKING) {
				classRosterArray[i] = new NetworkStudent(age, courseDays, studentID, email, firstName, lastName, degreeProgram);
			}
			else if (degreeProgram == SECURITY) {
				classRosterArray[i] = new SecurityStudent(age, courseDays, studentID, email, firstName, lastName, degreeProgram);
			}
			else if (degreeProgram == SOFTWARE) {
				classRosterArray[i] = new SoftwareStudent(age, courseDays, studentID, email, firstName, lastName, degreeProgram);
			}
			else {
				classRosterArray[i] = new Student(age, courseDays, studentID, email, firstName, lastName, degreeProgram);
			}

			break;//stop 
		}
	}
}

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[i]->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";
		}
	}
}

void Roster::remove(string studentID)
{
	bool studentRemoved = false;
	for (int i = 0; i < sizeof(classRosterArray) / sizeof(classRosterArray[i]); i++) {
		if (classRosterArray[i] != nullptr && classRosterArray[i]->fetchStudentId() == studentID) {
			classRosterArray[i] = nullptr;
			studentRemoved = true;
			break;
		}
	}

	if (studentRemoved == false) {
		cout << "ERROR: Student ID '" << studentID << "' was not found.";
	}
}

Roster::~Roster()
{
	delete * classRosterArray;
}
string Student::fetchEmail()
{
	return Email;
}


#include "Student.h"

//basic operation for return of requested information 

int Student::fetchAge()
{
	return Age;
}

int * Student::fetchDaysInCourse()
{
	return DaysInCourse;
}

void Student::mutateEmail(string email)
{
	Email = email;
}

string Student::fetchEmail()
{
	return Email;
}

string Student::fetchFirstName()
{
	return FirstName;
}

string Student::fetchLastName()
{
	return LastName;
}

string Student::fetchStudentId()
{
	return StudentId;
}

Degree Student::fetchDegreeType()
{
	return Degree();
}

void Student::mutateAge(int age)
{
	Age = age;
}


void Student::mutateDaysInCourse(int *daysInCourse)
{
	for (int i = 0; i < 3; i++) {
		DaysInCourse[i] = daysInCourse[i];
	}
}


void Student::mutateFirstName(string firstName)
{
	FirstName = firstName;
}

void Student::mutateLastName(string lastName)
{
	LastName = lastName;
}

void Student::mutateStudentId(string studentId)
{
	StudentId = studentId;
}

void Student::mutateDegreeType(Degree degreeType)
{
	DegreeType = degreeType;
}

Student::Student(int age, int daysInCourse[3], string studentId, string Email, string firstName, string lastName, Degree degreeType)
{
 mutateAge(age);
mutateStudentId(studentId);
mutateEmail(Email);
mutateFirstName(firstName);
mutateLastName(lastName);
mutateDegreeType(degreeType);
mutateDaysInCourse(daysInCourse);
mutateDegreeType(degreeType);
}

void Student::print()
{
	int *daysInCourse = fetchDaysInCourse();

	cout << fetchStudentId() << "\t";
	cout << "First Name: " << fetchFirstName() << "\t";
	cout << "Last Name: " << fetchLastName() << "\t";

	cout << "Age: " << fetchAge() << "\t";
	cout << "daysInCourse: " << daysInCourse[0] << "8" << daysInCourse[1] << "8" << daysInCourse[2] << "\t";

	cout << "Degree Program: ";
	switch (fetchDegreeProgram()) {
	case 0: cout << "Security";
		break;
	case 1: cout << "Networking";
		break;
	case 2: cout << "Software";
		break;
	case 4: cout << "Unknown or Unspecified";
		break;
	}
	
}

Degree Student::fetchDegreeProgram()
{
	return NODEGREE;
}

Student::~Student()
{
}


What I have tried:

I am getting a read access violation on the line Return Email; I have included the associated code from Student class as well. I am not really sure why it is running into a read access violation Email is apparently unable to read the characters from the string.
Posted
Updated 9-Nov-18 6:21am
v7
Comments
John R. Shaw 8-Nov-18 13:26pm    
Try "while (x < 9 && getline(ss, token, ','))" to start with.
gtyoung2 8-Nov-18 14:21pm    
That resolved that issue thank you! unfortunately, I have run into another error now reading for my fetch email function. It apparently has an issue reading the string, I have added this to the question above if you are willing to take a look
Richard MacCutchan 8-Nov-18 15:56pm    
Please edit your question with the updated code and explain exactly what the problem is and where it occurs.
gtyoung2 8-Nov-18 16:12pm    
done.
Richard MacCutchan 8-Nov-18 16:22pm    
Yes but you have not explained anything. We cannot guess what Email refers to.

1 solution

You need to use the debugger for solving that problem. Typical for your last problem is that the Student object is invalid like a null pointer or deallocated memory.

Set a break point some calls before that crash.
 
Share this answer
 

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