I can't seem to figure out how to do this. The code I have in there (RemoveAll function) keeps leading to many program crashes. Please help.
Friends.h
Code:
class linkedlist {
public:
char name[100];
int birthday;
int phone;
linkedlist* nextstudent;
linkedlist ();
void Insert();
void RemoveAll();
//void Reset();
//int* GetNext();
~linkedlist ();
};
Friends.cpp
Code:
#include <iostream>
#include "friends.h"
using namespace std;
int choice = 0;
int counter = 0;
linkedlist* student;
linkedlist* headstudent;
linkedlist* prevstudent = NULL;
linkedlist functions;
linkedlist::linkedlist () {
strncpy_s(name, "No Input", 9);
birthday = 0;
phone = 0; }
void linkedlist::Insert ()
{
student = new linkedlist;
if (prevstudent == NULL){
headstudent = student; }
else {
prevstudent->nextstudent = student; }
cout<<"\nEnter the friend's name: ";
cin>>student->name;
cout<<"Enter the friend's birthday: (MMDD) ";
cin>>student->birthday;
cout<<"Enter the friend's phone number: (No Dashes) ";
cin>>student->phone;
cout<<"\n\n";
prevstudent = student;
prevstudent->nextstudent = NULL;
student = headstudent;
}
void linkedlist::RemoveAll () {
student = headstudent;
do {
prevstudent = student;
student = student->nextstudent;
delete prevstudent;
} while (student != NULL);
}
linkedlist::~linkedlist () {
RemoveAll(); }
int Print()
{
cout<<"\n\n";
if (counter == 0) {
cout<<functions.name<<"\nBirthday: "<<functions.birthday<<"\nPhone #: "<<functions.phone<<"\n\n"; }
else {
do {
cout<<student->name<<"\nBirthday: "<<student->birthday<<"\nPhone: "<<student->phone<<"\n\n";
student = student ->nextstudent;;
} while (student != NULL);
student = headstudent; }
// for (int n = 0;n < total;n++) {
// cout<<"Name: "<<student->name<<"\nBirthday: "<<student->birthday<<"\nPhone #: "<<student->phone<<"\n\n"; }
return 0;
}
int main ()
{
while (choice != 4) {
cout<<"Please select an option...\n\n1. Enter Data\n2. Print Data\n3. Erase All Data\n4. Quit\n\n";
cin>>choice;
if (choice == 1){
functions.Insert(); }
if (choice == 2) {
Print(); }
if (choice == 3) {
functions.RemoveAll(); }
}
cout<<"\n";
system("PAUSE");
return 0;
}