Question: vector subscript out of range , Help me load this CSV file into vector so program runs successfully; Had to shorten code to that I
vector subscript out of range , Help me load this CSV file into vector so program runs successfully;
Had to shorten code to that I can Post
#include
using namespace std;
//Load Courses from cin choice for csv file
bool loadCourses(string courseFile, CourseBST* coursesBST) {
// Open course file, get and separate each line & insert into BST // ----------------------------------------------------------------------
try { ifstream courseFile; courseFile.open("ABC Advising Program Input.csv"); if (courseFile.is_open()) { while (!courseFile.eof()) {
// A vector will hold each csv that a course contains // ---------------------------------------------------------------------- vector
// Get substring of each course data, add to vector & delete from str // ----------------------------------------------------------------------
unsigned int comma = courseData.find(','); if (comma < 100) {
// Any data field is allowed 99 chars or less // ----------------------------------------------------------------------
courseInfo.push_back(courseData.substr(0, comma)); courseData.erase(0, comma + 1); } // Add the last course after the final comma else { courseInfo.push_back(courseData.substr(0, courseData.length())); courseData = ""; } } // Load the separated values into a course, //insert into Binary Search Tree and close file Course course; course.courseNum = courseInfo[0]; course.courseName = courseInfo[1]; for (unsigned int i = 2; i < courseInfo.size(); i++) { course.preReqs.push_back(courseInfo[i]); } coursesBST->Insert(course); } courseFile.close(); return true; } } catch (runtime_error& e) { cerr << e.what() << endl; } return false; }
// Main method int main(int argc, char* argv[]) {
// Process the command line argument
string courseFile, courseId; switch (argc) { case 2: courseFile = argv[1]; break; case 3: courseFile = argv[1]; courseId = argv[2]; break; default: courseFile = ""; break; } // Define Binary Search Tree and Welcome User CourseBST* coursesBST = nullptr; cout << " Welcome to the course planner! " << endl;
// Making user choice a string and converting first char to int avoids invalid data
string choice = "0"; int userChoice = choice[0] - '0';
//if not 9 or exit while (userChoice != 9) {
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? ";
cin >> choice;
// Check if user choice is a double digit
if (choice.length() == 1) userChoice = choice[0] - '0'; else userChoice = 0; bool success = 1;
// Handle users choice from menu
switch (userChoice) {
// Instantiate Binary Search Tree // get file path name //and load courses into Binary Search Tree case 1: if (coursesBST == nullptr) coursesBST = new CourseBST(); if (courseFile.length() == 0) { cout << "Enter the file name that contains the course data: "; cin >> courseFile; }
// Determine if the file was opened- load successful
success = loadCourses(courseFile, coursesBST); if (success) cout << "Courses have been loaded. " << endl; else cout << "File not found. " << endl; courseFile = ""; break;
// Validate that a Binary Search Tree exists //and print a sample schedule case 2: if (coursesBST != nullptr && success) { cout << "Here is a sample schedule: " << endl; coursesBST->PrintSampleSchedule(); cout << endl; } else cout << "Load courses first - option 1 " << endl; break;
// Validate that a Binary Search Tree exists //and search/ display course info
case 3: if (coursesBST != nullptr && success) { if (courseId.length() == 0) { cout << "What course do you want to know about? "; cin >> courseId; for (auto& userChoice : courseId) userChoice = toupper(userChoice); } coursesBST->PrintCourseInformation(courseId); cout << endl; } else cout << "Load courses first - option 1 " << endl; courseId = ""; break;
// User entered invalid data or exited the program
default: if (userChoice != 9) cout << choice << " is not a valid option " << endl; break; } } cout << " Thank you for using the course planner!" << endl; return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
