Question: I ' m having a hard time getting this to open and read a user inputted file name. If I hard code the filename into

I'm having a hard time getting this to open and read a user inputted file name. If I hard code the filename into the code it can read it no problem, so I can't tell if it's my code or if it just can't reach my file. It also won't just print the 'can't open file' error message and bring me back to the menu. It prints infinite menus and I have to crash it. Can you please suggest a solution?
#include
#include
#include
#include
#include
#include
using namespace std;
//course structure
struct Course {
string courseId;
string title;
vector prerequisites;
};
//function to split a string by a delimiter
vector split(const string& str, char delimiter){
vector tokens;
stringstream ss(str);
string token;
while (getline(ss, token, delimiter)){
tokens.push_back(token);
}
return tokens;
}
//function to load course data into hash table
void loadCourses(const string& filename, unordered_map& courseTable){
ifstream file(filename);
if (!file.is_open()){
cerr << "Error: Could not open file" << endl;
return;
}
string line;
while (getline(file, line)){
vector parts = split(line,',');
if (parts.size()<2){
cerr << "Error: Invalid data format in file" << endl;
continue;
}
Course course;
course.courseId = parts[0];
course.title = parts[1];
for (size_t i =2; i < parts.size(); ++i){
course.prerequisites.push_back(parts[i]);
}
courseTable[course.courseId]= course;
}
file.close();
cout << "Courses loaded successfully" << endl;
}
//function to print alphanumeric list of all courses
void printCourseList(const unordered_map& courseTable){
vector courseIds;
for (const auto& entry : courseTable){
courseIds.push_back(entry.first);
}
sort(courseIds.begin(), courseIds.end());
cout << "Here is a sample schedule:" << endl;
for (const string& courseId : courseIds){
cout << courseId <<","<< courseTable.at(courseId).title << endl;
}
}
//function to print specific course and course prerequesites
void printCourse(const unordered_map& courseTable, const string& courseId){
auto it = courseTable.find(courseId);
if (it == courseTable.end()){
cout << "Error: Course not found" << endl;
return;
}
const Course& course = it->second;
cout << course.courseId <<","<< course.title << endl;
cout << "Prerequisites: ";
if (course.prerequisites.empty()){
cout << "None";
}
else {
for (size_t i =0; i < course.prerequisites.size(); ++i){
cout << course.prerequisites[i];
if (i < course.prerequisites.size()-1){
cout <<",";
}
}
}
cout << endl;
}
//main program
int main(){
unordered_map courseTable;
int choice;
string filename;
cout << "Welcome to the course planner." << endl;
do {
cout <<"
1. Load Data Structure." << endl;
cout <<"2. Print Course List." << endl;
cout <<"3. Print Course." << endl;
cout <<"9. Exit
"<< endl;
cout << "What would you like to do?"<< endl;
cin >> choice;
switch (choice){
case 1:
cout << "Enter the file name: ";
cin >> filename;
loadCourses(filename, courseTable);
break;
case 2:
if (courseTable.empty()){
cout << "Error: No data loaded. Please load data structure first." << endl;
}
else {
printCourseList(courseTable);
}
break;
case 3:
if (courseTable.empty()){
cout << "Error: No data loaded. Please load data structure first." << endl;
}
else {
string courseId;

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!