Hello. I can't seem to figure out why my program is exiting when I try to insert into my linked list.
I'm sure it's something i keep overlooking, but I need a second pair of eyes to help me out. Thanks for any insight.
This is my test code.
Code:
#include <iostream>
#include "list.h"
using namespace std;
void addinput(list &);
void removeinput(list &);
void displayinput(list &);
int main()
{
int choice = 0;
int mainloop = 0;
list mylist;
while(mainloop == 0)
{
//display GUI for user
cout << "==============================*" << endl;
cout << "WELCOME STEVEN *" << endl;
cout << "==============================*" << endl;
cout << "PRESS 1 TO ADD AN ENTRY *" << endl;
cout << "PRESS 2 TO REMOVE AN ENTRY *" << endl;
cout << "PRESS 3 TO DISPLAY ENTRIES *" << endl;
cout << "PRESS 4 TO EXIT *" << endl;
cout << "===============================" << endl;
cin >> choice;
cin.get();
switch(choice)
{
case 1: addinput(mylist);
break;
case 2: removeinput(mylist);
break;
case 3: displayinput(mylist);
break;
case 4: mainloop++;
} break;
}
return 0;
}
void addinput(list & mylist)
{
int numadd = 0;
cout << "TYPE THE NUMBER YOU WISH TO ADD" << endl;
cin >> numadd;
cin.get();
mylist.add(numadd);
return;
}
void removeinput(list & mylist)
{
int numremove = 0;
cout << "TYPE THE NUMBER YOU WISH TO REMOVE" << endl;
cin >> numremove;
cin.get();
mylist.remove(numremove);
return;
}
void displayinput(list & mylist)
{
mylist.display();
return;
}
Here is my list code.
Code:
#include <iostream>
using namespace std;
#include "list.h"
//=========================================================
list::list()
{
num_in_list = 0;
head = NULL;
}
//==========================================================
list::~list()
{
node * temp; //temporary pointer for deleting
//deletes vacation photo list
while(head)
{
temp = head->next;
delete head;
head = temp;
}
}
//==========================================================
int list::add(int & numadd)
{
node * newNode;
newNode = new node;
newNode->item = numadd;
// insert at the front of the list
newNode->next = head;
// move the head of the list to the new node
head = newNode;
++num_in_list;
return 0;
}
//==========================================================
int list::remove(int & numremove)
{
return 0;
}
//==========================================================
int list::display()
{
node * temp; // our current node (position)
// begin at the head of the list
temp = head;
// loop until done
while(temp)
{
cout << temp->item << endl;
temp = temp->next;
}
return 0;
}