0

I'm having trouble understanding Linked Lists. I am trying to print to standard output all the items in the list, but only the last record is being printed out. I am not sure what is wrong with my code. I believe the head pointer is only storing one record and is being overwritten each time I put data into a new record, but I don't know where the issue lies exactly and I'm not sure how to rectify it because I can't see where the code is wrong. Please let me know if you can see the issue with my code, and how I can fix it. Thank you.

My int main function is small, but I believe all the necessary information is there. Here is my int main:

int main(void){

sPtr head;
head=NULL;
cout<<putIntoList(head);
system("pause");
return 0;
}    

The struct looks like this:

struct drugData{
 string name;
 string disease;
 int effectiveness;
 drugData *next;
 drugData(){
  effectiveness=0;
  name="n";
  disease="n";
  next=NULL;
 }
}

And here is the typedef to use for drugData types as function parameters.

typedef drugData* sPtr;

This is the first function, which reads data from a file into the fields of the struct drugData.

void putIntoList(sPtr &head){

(code to open file is located here)

sPtr structPointer=new drugData;
while (!inFile.eof()){
getline(inFile,tempDrugName, ',');
getline(inFile,tempDisease,',');
inFile>>tempEff;

structPointer->name=tempDrugName;
structPointer->disease=tempDisease;
structPointer->effectiveness=tempEff;
cout<<"Drug:"<<structPointer->name;

approvalGetter(structPointer);//This function asks the user for input on each drug.
}
headInsert(structPointer, head);//This function inserts new objects at head.


menuOptions(structPointer, head);//This function presents a menu to either print, search, or quit.

inFile.close();
}//End of putIntoList function.

Here is the code for the function called headInsert:

void headInsert(sPtr &newItem, sPtr& head){
newItem->next=head;
head=newItem;
newItem=NULL;
}

And here is the code for the menuOptions function:

void menuOptions(sPtr& pointer, sPtr& head){
int menuAnswer=0;
cout"Choose a menu option:"<<endl<<"(1) Print"<<endl<<"(2) Search"<<endl<<"(3) Quit"<<endl;
cin>>menuAnswer;
if (menuAnswer==1){
for (pointer=head; pointer!=NULL; pointer=pointer->next){
cout<<pointer->drugName;
}
}//Included here is the menu options 2 and 3.
}

I expect the output to be the names of the drugs contained in the file, of which there are six, but the only output I see is the name of the last drug in the file.

  • 1
    You only allocate one dynamic object, and reuse it repeatedly in your loop. Naturally the last thing you stuffed into it will be what you get. Perhaps the method inserting data into the list should be the thing allocating the new objects. – WhozCraig Apr 02 '19 at 22:22
  • 2
    Always try to develop new functionality *in isolation.* For instance, when you try to get your first linked list working, do so with hard-coded `int` values, not complex structures read from a file. – Beta Apr 02 '19 at 22:25
  • Unrelated to your problem, but please read [Why is iostream::eof inside a loop condition considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – Some programmer dude Apr 02 '19 at 22:30
  • And to add to the comment from @Beta, don't write large patches of code untested. Write a very small piece of easily testable code. Build and test it. Then write another small piece of code, build and test it. Etc. If there's a problem it will be much easier to isolate it, debug it, and find out what the problem is. – Some programmer dude Apr 02 '19 at 22:32
  • 2
    Please indent your code. How are we (or you) supposed to read it? – Paul Sanders Apr 02 '19 at 22:33

0 Answers0