Search code examples
c++listinputvisual-studio-debuggingcontiguous

Somehow my values don't seem to all be inserting into a list


I am doing a homework assignment where I am supposed to use our professors contiguous list class to store a list of personal record which can then be printed or searched for a specific record. The personal record struct contains only member data for a first_name, last_name and int code.

My problem is inserting the records. We have to insert in correct alphabetical order, and any records with the same first and last name are discarded. My code is below:

         string input;
         cout << endl << "Enter Data File Name:" << endl;
         getline(cin, input);
         ifstream insertion_file;
         insertion_file.open(input.c_str());
         if(!insertion_file.fail()){
            record_list.clear();

            while(!insertion_file.fail() && !insertion_file.eof()){
               Personal_record input_rec;
               string code_string;
               getline(insertion_file, input_rec.last_name);
               getline(insertion_file, input_rec.first_name);
               getline(insertion_file, code_string);
               input_rec.code = string_to_int(code_string);


               //implementation of requirement 1
               if (record_list.empty()) record_list.insert(0, input_rec);
               else { 
                   int i = 0;
                   Personal_record temp;
                   //while loop increments i and retrieves a record until input_rec.last_name is not smaller than temp.last_name
                   do {
                       record_list.retrieve(i, temp);
                       i++;
                   } while (input_rec.last_name < temp.last_name && i <= record_list.size());

                   //if last_names are the same, check first names
                   if (input_rec.last_name == temp.last_name) {
                       while (input_rec.first_name < temp.first_name) record_list.retrieve(++i, temp);
                       //if last names are the same, only insert if there is no matching first name
                       if (input_rec.first_name != temp.first_name) record_list.insert(i, input_rec);
                   }

                   //if last name is not the same, insert
                   else record_list.insert(i, input_rec);                 
               }
            }
         } else
            cout << "Invalid file name." << endl;

Only the code after the comment "implementation of requirement 1" is mine, the rest is professor code that cannot be altered.

I am not getting any compiler errors, but the program seems to freeze somewhere in the process. After inserting the records from the file it should return control back to the user to enter a command, but this never happens. I am trying to use the Visual C++ debugger, but I am unfamiliar with it and it isn't giving me much insight. Any help is greatly appreciated!


Solution

  • You are accessing both element 0 and element size(). Unless size() in your program actually means size - 1, that's a problem.