parsing a comma delimited string WITHOUT using vectors

wondering if someone could assist with this. I have the following code; however, I need to change this to work without using vectors. I'm currently stumped on how to do this. I see several topics on here about this but it seems everyone just mentions vectors. The parseStudentID section is what I need to change.

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
 #include "roster.h"

Roster::Roster(int maxSize) {
	const string studentData[] = {  //Provided Data Table
		"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,Chris,Riedel,criede6@wgu.edu,44,15,31,45,SOFTWARE"
	};

	this->lastIndex = -1;
	this->maxSize = maxSize;
	this->students = new Student * [maxSize]; // create new pointer array to hold student objects

	for (int i = 0; i < maxSize; ++i) {

		ParseStudentId(studentData[i]);
		if (degreeType == "SECURITY") {
			students[i] = new SecurityStudent(studentId, firstName, lastName, email, age, daysInCourse, SECURITY);
			++lastIndex;
		}
		else if (degreeType == "NETWORK") {
			students[i] = new NetworkStudent(studentId, firstName, lastName, email, age, daysInCourse, NETWORKING);
			++lastIndex;
		}
		else if (degreeType == "SOFTWARE") {
			students[i] = new SoftwareStudent(studentId, firstName, lastName, email, age, daysInCourse, SOFTWARE);
			++lastIndex;
		}
		else {
			cout << "Degree not found" << endl;
		}
	}
}

Roster::~Roster() {
	cout << "Roster destructor called" << endl;
	delete[] students;
}

void Roster::ParseStudentId(string studentData) {
	stringstream studentSS(studentData);
	vector<string> studentDataVector;

	for (int i = 0; i < 9; ++i) {	//parse string
		string substr;
		getline(studentSS, substr, ',');
		studentDataVector.push_back(substr);
	}

	studentId = studentDataVector.at(0);	//set to data members
	firstName = studentDataVector.at(1);
	lastName = studentDataVector.at(2);
	email = studentDataVector.at(3);
	age = studentDataVector.at(4);
	this->daysInCourse = new int[3];
	for (int i = 0; i < 3; ++i) this->daysInCourse[i] = stoi(studentDataVector.at(i + 5));
	degreeType = studentDataVector.at(8);
	return;
- Replace vector<string> studentDataVector; with string studentData[9];?
- Replace push_back(substr); with [code]studentData[i] = substr;[code]
- Replace the studentDataVector.at(index) calls with studentData[index] calls.
Last edited on
Don't use a loop:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void Roster::ParseStudentId(string studentData)
{
    stringstream studentSS(studentData);

    getline(studentSS, studentId, ',');
    getline(studentSS, firstName, ',');
    getline(studentSS, lastName, ',');
    getline(studentSS, email, ',');
    getline(studentSS, age, ',');

    this->daysInCourse = new int[3];
    for (int i = 0; i < 3; ++i) {       //parse string
        char ch;
        studentSS >> daysInCourse[i] >> ch;
        if (ch != ',') {
            // failed
        }
    }
    getline(studentSS, degreeType, ',');
}


Why is daysInCourse a pointer instead of simply an array of 3 ints? Does it ever NOT point to 3 ints?
Topic archived. No new replies allowed.