#include
#include
#include
#include
#include
<sstream>
<string>
<vector>
CS 300 Analysis & Design Project Two (7-1)
<iostream>
<fstream>
SNERD
#include <map>
#include <algorithm>
using namespace std;
/*
* Course structure
* Stores course number, title, and prerequisites
*/
struct Course {
string courseNumber;
string courseTitle;
vector<string> prerequisites;
};
/*
* Converts a string to uppercase
* Ensures consistent searching and sorting
*/
string toUpper(string str) {
transform(str.begin(), str.end(), str.begin(), ::toupper);
return str;
}
/*
* Loads course data from input.txt into the map
* Returns true if successful
*/
bool loadCourses(const string& fileName, map<string, Course>& courses) {
ifstream file(fileName);
// Verify file opened correctly
if (!file.is_open()) {
cout << "ERROR: Unable to open file." << endl;
return false;
}
courses.clear();
, TESTBANK
string line;
// Read each line of the file
while (getline(file, line)) {
if (line.empty()) continue;
stringstream ss(line);
SNERD
string token;
vector<string> tokens;
// Split line by commas
while (getline(ss, token, ',')) {
tokens.push_back(token);
}
// Must have at least course number and title
if (tokens.size() < 2) continue;
Course course;
course.courseNumber = toUpper(tokens[0]);
course.courseTitle = tokens[1];
// Store prerequisites, if any
for (size_t i = 2; i < tokens.size(); i++) {
course.prerequisites.push_back(toUpper(tokens[i]));
}
// Insert course into map (auto-sorted)
courses[course.courseNumber] = course;
}
file.close();
cout << "Data loaded successfully." << endl;
return true;
}
/*
* Prints all courses in alphanumeric order
*/
void printCourseList(const map<string, Course>& courses) {
cout << "Here is a sample schedule:" << endl;
// Map automatically sorts keys
for (const auto& pair : courses) {
cout << pair.second.courseNumber << ", "