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
Im 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 splitconst string& str char delimiter
vector tokens;
stringstream ssstr;
string token;
while getliness token, delimiter
tokens.pushbacktoken;
return tokens;
function to load course data into hash table
void loadCoursesconst string& filename, unorderedmap& courseTable
ifstream filefilename;
if file.isopen
cerr "Error: Could not open file" endl;
return;
string line;
while getlinefile line
vector parts splitline;
if partssize
cerr "Error: Invalid data format in file" endl;
continue;
Course course;
course.courseId parts;
course.title parts;
for sizet i ; i parts.size; i
course.prerequisites.pushbackpartsi;
courseTablecoursecourseId course;
file.close;
cout "Courses loaded successfully" endl;
function to print alphanumeric list of all courses
void printCourseListconst unorderedmap& courseTable
vector courseIds;
for const auto& entry : courseTable
courseIds.pushbackentryfirst;
sortcourseIdsbegin courseIds.end;
cout "Here is a sample schedule:" endl;
for const string& courseId : courseIds
cout courseId courseTable.atcourseIdtitle endl;
function to print specific course and course prerequesites
void printCourseconst unorderedmap& courseTable, const string& courseId
auto it courseTable.findcourseId;
if it courseTable.end
cout "Error: Course not found" endl;
return;
const Course& course itsecond;
cout course.courseId course.title endl;
cout "Prerequisites: ;
if courseprerequisites.empty
cout "None";
else
for sizet i ; i course.prerequisites.size; i
cout course.prerequisitesi;
if i course.prerequisites.size
cout ;
cout endl;
main program
int main
unorderedmap courseTable;
int choice;
string filename;
cout "Welcome to the course planner." endl;
do
cout
Load Data Structure." endl;
cout Print Course List." endl;
cout Print Course." endl;
cout Exit
endl;
cout "What would you like to do endl;
cin choice;
switch choice
case :
cout "Enter the file name: ;
cin filename;
loadCoursesfilename courseTable;
break;
case :
if courseTableempty
cout "Error: No data loaded. Please load data structure first." endl;
else
printCourseListcourseTable;
break;
case :
if courseTableempty
cout "Error: No data loaded. Please load data structure first." endl;
else
string courseId;
