-3

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!

Victoria Potvin
  • 127
  • 1
  • 11
  • 2
    Did you try to narrow it down? To debug it? http://sscce.org – Lightness Races in Orbit Jun 06 '14 at 15:42
  • 1
    BTW if the code before "implementation of requirement 1" is your professor's, then you have a problem because I can spot three serious bugs with his code right off the bat. – Lightness Races in Orbit Jun 06 '14 at 15:43
  • 1
    Wow that's a lot of duplication of calls to the `insert` function. You might find it useful to use a variable to store a `found` flag, and then have `if (!found) list.insert()` exactly once. – Ben Voigt Jun 06 '14 at 15:43
  • You need to place a breakpoint (F9), the program stops there and then you can view it's state (variables, etc). It's not possible to debug a **running** program. – Agent_L Jun 06 '14 at 15:43
  • 1
    @LightnessRacesinOrbit: For example, not checking the good bit after calling `getline()` three times? – Ben Voigt Jun 06 '14 at 15:43
  • @BenVoigt: For example, yes. `while(!insertion_file.fail() && !insertion_file.eof())` I don't even – Lightness Races in Orbit Jun 06 '14 at 15:45
  • 1
    @Lightness: Perhaps `while (getline(getline(getline(insertion_file, input_rec.last_name), input_rec.first_name), code_string))` could fix that problem while maintaining the professor's (lack of) style? – Ben Voigt Jun 06 '14 at 15:48
  • This program aside, if indeed everything above `//implementation of requirement 1` is your professor provided code, your first *big* problem is finding a prof that knows wth they're doing. [Spencer's **Sixth** Commandment](http://www.lysator.liu.se/c/ten-commandments.html), doesn't just apply to C programmers. – WhozCraig Jun 06 '14 at 15:49
  • @Whoz: The errors aren't ignored, they're just checked at the wrong time. A *very* common question here on SO, I might add. – Ben Voigt Jun 06 '14 at 16:49
  • @BenVoigt maybe i'm not seeing the same code you are, but the "check" you're referring to is one where the last record read-*failure* is "handled" by ignoring duplicate entries in the list rather detecting the read-failure in the first place. Where this a list that supported duplicates, it would clearly be wrong. My point was simply the prof accounted for no error handling, and should have. Relying on the rest of the algorithm to pick up the pieces (by happenstance in this case) isn't a solution. I'm also genuinely curious what `string_to_int` returns when passed an *empty* `std::string`. – WhozCraig Jun 06 '14 at 18:35

1 Answers1

1

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

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720