I have a programming problem I have been working on all day, and I have posted q
ID: 3741992 • Letter: I
Question
I have a programming problem I have been working on all day, and I have posted questions on forums with nothing but negative results and comments from others. Yes, I am still new to C++ and I am doing my best to learn the material and apply it in a way that not only works, but makes sense. That being said it is most likely my unfamiliarity with C++ that has led to my problem (according to those that commented on my forum posts). Everything in my program is operating perfectly except for one function. The "swap2" function in my Course_Directory.cpp will not work and throws an error "error: terminate called after throwing an instance of 'std::length_error' what() basic_string::_M_create" and prematurely exits the program. I have tried setting the value to a temporary variable, and I've tried using swap() with no luck. Could someone please help me get this last function to work? If it is not possible with the way I have the code written, could someone help me with getting the proper code in place so that the function will operate correctly? I will include the .cpp and .h files for reference.
1 2 3 4 5 #ifndef COURSE DIRECTORY H #de fine COURSE DIRECTORY H #include using namespace std; class Course Directoryi 7 8 9 10 private: int* numberOfDepts string filename; char courseName [1000] int courseNum; int numStudents; int IDs [10001 int choice, 12 13 14 15 16 17 18 19 20 21 public Course Directory) //Course Directory (Gonsi Course Directorys original) Course Directory) void run (string) void displayMenu) void coursesInDept (string) void studentsInCourse) void largestClass O; void swap2 ) void schedule ) void printAllCourses ) 23 24 25 26 27 28 29 30 #endif //COURSE DIRECTORY HExplanation / Answer
In C/C++ Programming, Two character array directly never comparable, instead of that we are using strcmp() by string.h or cstring library
if you change to 166 and 168 line, it will works file
if(course[i].courseNum == courseNo && strcmp(courses[i].courseName, dept) ==0)
firstIndex = 0;
if(course[i].courseNum == courseNo2 && strcmp(courses[i].courseName, dept2) ==0)
secondIndex = 0
and in swap function automatically swaps data if it is correct
Let me know if you have any clarifications. Thank you...
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.