c++ - possible misunderstanding of map iterators with for loops -


very specific question i'm afraid (and i'm rather novice, apologies in advance):

i'm trying finish final project university object-oriented c++ course. i'm creating student database store exam results students. setup has loads of custom classes work (or @ least want them do).

the project set follows:

i have "master" map of "course"s, points (so course isn't duplicated if more 1 student taking it).

a "student" vector of pointers "course"s , corresponding double "result", , have master map of students in system.

a "degree" class of 2 vectors of pointers, 1 courses offered degree, , 1 students taking degree. when degree created, searches both master maps. if first x letters in course id matches degree prefix, course added. if student's subject matches course name, student added.

my problem this:

as have options manually input courses , students after initial setup csv files, have writen function update degrees if course/result added should included in degree (see below). however, code inevitably results in first course , student being re-added (i.e. repeated) first degree first time function called. problem not repeated if function called again. have absolutely no idea why. huge amount of time , cout statements later , i'm no closer solving this. missing obvious first run? may have set loops wrong (i'm not familiar maps). don't hesitate call me idiot!

as have said above, rest of program gravy, without odd issue program fine. problem not appear come print functions either.

thank in advance time.

//upgrade degrees function: used whenever new courses or students created user. ticks through stored degrees , scans cd , sd. if finds unstored course or student should stored, added.  void degree_database::update_degrees(course_database &cd, student_database &sd) {     cout << "updating degrees..." << endl;      bool found = false;     vector<degree>::iterator current;     (current = start; current < end; ++current) {              //scan course list         map<string, course>::iterator x;         (x = cd.get_start(); x != cd.get_end(); ++x) {             if (x->first.substr(0,3) == current->get_prefix().substr(0,3) || current->get_prefix() == "all") {                  //check see if course stored                 vector<course*>::iterator a;                 (a = current->get_c_start(); < current->get_c_end(); ++a) {                     if (*a == &(x->second)) {                         found = true;                         break;                     }                 }                  //if found == true, while loop broke (i.e. course stored).                 if (found == false) current->add_course(x->second);                 found = false;             }         }          //scan student list         found = false;         map<string, student>::iterator y;         (y = sd.get_start(); y != sd.get_end(); ++y) {             if (y->second.get_subject() == current->get_name() || current->get_name() == "all") {                  //check see if course stored                 vector<student*>::iterator b;                 (b = current->get_s_start(); b < current->get_s_end(); ++b) {                     if (*b == &(y->second)) {                         found = true;                         break;                     }                 }                  //if found == true, while loop broke (i.e. student stored).                 if (found == false) current->add_student(y->second);                 found = false;             }         }      }      cout << "done." << endl;  } 

you store course value in course list , use pointer object in comparison. apparently, shoud store pointers in map. think (*a == &(x->second)) fails on first run , pointer object course map added degree object. on second run, (*a == &(x->second)) succeeds , looks ok. same student map.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -