I am creating a program for my class project that reads in a string text file, parses the data into objects then stores into a vector. The file is simply a course number, name, and any prerequisites of the course (if they have any) -- EXAMPLE: CSCI350,Operating Systems,CSCI300
I am running into a bit of a snag when it comes to displaying all of the courses and have not been able to find a solution. There are two courses that do not have any prerequisites, and when printing the sample schedule a new line is added right after each of those courses. I believe the issue lies either within the prerequisites vector or my getline().
If anyone could explain to me what I am doing wrong that would be lovely, as I am learning on my own. Any help or tips are greatly appreciated and thank you in advance. I included my print functions for the sample schedule and for reference my function to print just prerequisites.
OUTPUT EXAMPLE:
MATH201, Discrete Mathematics //Space below this
CSCI300, Introduction to Algorithms
CSCI350, Operating Systems
CSCI101, Introduction to Programming in C++
CSCI100, Introduction to Computer Science //Space below this
CSCI301, Advanced Programming in C++
CSCI400, Large Software Development
CSCI200, Data Structures
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
|
struct Course {
string courseNumber;
string name;
vector<string> prerequisites;
};
vector<Course> loadAndParseData() {
ifstream inFS("abcu.txt");
string line;
vector<Course> courses;
while (getline(inFS, line)) {
istringstream iss(line);
string substring;
vector<string> substrings;
while (getline(iss, substring, ',')) {
substrings.push_back(substring);
}
Course course;
course.courseNumber = substrings[0];
course.name = substrings[1];
// //Adds prerequisites
for (int i = 2; i < substrings.size(); ++i) {
course.prerequisites.push_back(substrings[i]);
}
courses.push_back(course); //Adds course to vector
}
inFS.close();
return courses;
}
void printSampleSchedule(vector<Course> courses) {
cout << "Here is a sample schedule:" << endl;
for (int i = 0; i < courses.size(); ++i) {
cout << courses[i].courseNumber << ", " << courses[i].name << endl;
}
}
void printPrerequisites(Course course) {
vector<string> prerequisites = course.prerequisites;
cout << "Prerequisites: ";
for (int i = 0; i < prerequisites.size(); ++i) {
cout << prerequisites[i] << ",";
}
}
|