#include <string>
using namespace std;
class PersonList
{
private:
char aName[7];
int aBribe;
PersonList *link;
public:
void addNodes();
void display();
};
#include <iostream>
#include <string>
using namespace std;
#include "mylink.h"
void PersonList::addNodes()
{
PersonList *temp2;
PersonList* startPtr = new PersonList();
PersonList* current = new PersonList();
PersonList *temp = new PersonList();//created the first node on the list
cout<<"Enter the person's name: ";
cin>>temp->aName;
cout<<"Enter the person's contribution: ";
cin>>temp->aBribe;
temp->link=NULL;//when i get last node, link will point to null(where am i in list?)
if(startPtr==NULL)
{
startPtr = temp;
current = startPtr;
}
else
{
temp2 = startPtr;
while(temp2->link!=NULL)
temp2 = temp2->link;
temp2->link=temp;
}
//}
}
void PersonList::display()
{
PersonList *temp;
PersonList *startPtr = this;
temp=startPtr;
while(temp != NULL)
{
cout<<temp->aName<<"\t\t"<<temp->aBribe<<endl;
temp = temp->link;
}
}
#include <iostream>
#include "mylink.h"
using namespace std;
int displayMenu (void);
void processChoice(int, PersonList&);
int main()
{
int num;
PersonList myList;
do
{
num = displayMenu();
if (num != 3)
processChoice(num, myList);
} while (num != 3);
return 0;
}
int displayMenu(void)
{
int choice;
cout << "\nMenu\n";
cout << "==============================\n\n";
cout << "1. Add student to waiting list\n";
cout << "2. View waiting list\n";
cout << "3. Exit program\n\n";
cout << "Please enter choice: ";
cin >> choice;
cin.ignore();
return choice;
}
void processChoice(int choice, PersonList& p)
{
switch(choice)
{
case 1: p.addNodes();
break;
case 2: p.display();
break;
}
}
My question is the display function is not displaying name and contribution that I enter.
Im using temp variable as a pointer node to call aName and aBribe. This goes through the list while it has not reached null. Nothing shows in output
You are creating a new list:
and then showing that. So, naturally it is empty.
You have a similar problem in your addNodes method. You are adding nodes to a new list, then throwing it away, which is actually a memory leak.