-1

So I'm working on an assignment for my class in which I have to read in data from a file and create a doubly linked list with it. I have all the difficult stuff done, now I'm just running into the problem where my program throws a bunch of random characters and kills itself on the last line.

Here is the function that is reading in the data and inserting it into my link list. My professor wrote this, so to be frank, I don't understand it very well.

void PropogateTheList(ifstream & f, LinkList & ML)
{
  static PieCake_struct * record;
  record= new PieCake_struct;
  f>>record->id>>record->lname>>record->fname>>record->mi>>record->sex>>record->pORc;

  while(!f.eof())
  {
    ML.Insert(record);
    record = new PieCake_struct;
    f>>record->id>>record->lname>>record->fname>>record->mi>>record->sex>>record->pORc;
  }
}

Here is the data that is being propagated:

1   Abay         Harege N O C
2   Adcock       Leand R F P
3   Anderson     Logan B M P
5   Bautista     Gloria A F P
10  Beckett      Dallas B F C
12  Ambrose      Bridget C F C
13  Beekmann     Marvin D M P
14  Bacaner      Tate D M C
16  Bis          Daniel F M P
18  Dale         Kaleisa G F C
19  DaCosta      Ricardo H M P
23  Adeyemo      Oluwanifemi I M C
24  Berger       Chelsea J F C
38  Daniels      Jazmyn K F P
39  Davis        Takaiyh L F C
40  DeJesus      Gabriel M M P
51  Castro       Floriana N F P
52  Chen         Justin O M C
53  Clouden      Ariel P F P
54  Conroy       Cameron Q M C
61  Contreras    Dominic R M P
62  Cooley       Kyle S M C
63  Creighton    Cara T F P
64  Cullen       William U M C
66  Blakey       Casey V M  C
67  Barbosa      Anilda W F P
83  Brecher      Benjamin X M P
84  Boulos       Alexandre Y F C
85  Barrios      Joshua Z M C
85  Bonaventura  Nash A M P
86  Bohnsack     David B M C
87  Blume        Jeffrey C M P
90  Burgman      Megan D F C
91  Bursic       Gregory E M P
92  Calvo        Sajoni F F C
93  Cannan       Austin G M  P
94  Carballo     Nicholas H M C
99  AlbarDiaz    Matias I F P

Currently, I sort the data alphabetically based off the last name, so on about the 5th line, when it tries to print out number 99 (AlabraDiaz) it dies. If I sort the list another way, the program always messes up with whatever the last line of data is. Any help would be great!

UPDATE:

So I've tried implementing an if(!.eof()) before inserting but it unfortunately doesn't do anything. I deleted the last of data, thus making the person Carballo. This is what my function prints out:

****** The CheeseCake Survey ****** 
Id      Last Name       First Name      MI      Sex     Pie/Cake
--      --------        ---------       --      ---     --------
2       Adcock
23      Adeyemo
12      Ambrose
3       Anderson
14      Bacaner
67      Barbosa
85      Barrios
5       Bautista
10      Beckett
13      Beekmann
24      Berger
16      Bis
66      Blakey
87      Blume
86      Bohnsack
85      Bonaventura
84      Boulos
83      Brecher
90      Burgman
91      Bursic
92      Calvo
93      Cannan
0       Carballo????NicholasA8?zL`8?zL`A8?zL`8?zL`??
Gilbert22
  • 47
  • 5
  • Will you provide any errors that you are getting? – Wiingreen Feb 26 '20 at 13:55
  • 4
    Your professor needs to read [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons). A deliberate memory leak is not an acceptable workaround for that problem. (And that `static` variable is just plain weird.) – molbdnilo Feb 26 '20 at 13:56
  • Does this answer your question? [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Marek R Feb 26 '20 at 14:04
  • 1
    Your professor should not be teaching C++. Stuff like that really explains some of the other questions here. – Sam Varshavchik Feb 26 '20 at 14:05
  • I updated the code and tried an if check, I also added the specific output I'm getting. – Gilbert22 Feb 26 '20 at 15:22

1 Answers1

1

Wouldn't it be better if you'd read from the stream first, then check if it's in eof state and based on that you'd insert the element? I'm writing the following code without compiler's help, here in the edit box, so apologises if I'd made any mistake. Of course the question arises or something I'd think of is what will happen if you'll try to read from your f stream in case where eof results in true. To read more about it you can check the following link: Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?

void PropogateTheList(ifstream & f, LinkList & ML)
{
    while(!f.eof())
    {
        static PieCake_struct * = new PieCake_struct;
        f>>record->id>>record->lname>>record->fname>>record->mi>>record->sex>>record->pORc;
        if(!f.eof())
            ML.Insert(record);
    }
}
Eric
  • 1,308
  • 12
  • 29