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.