My homework is to use a linked list for tracking friends b-days. I am having problems when inserting a new friend. It must be done in order using there b-days. When I call my function I prompt for data and insert the it using.
-----------FULL CODE BELOW--------------
FILENAME = inter.h Interface file
// Implementation filename = implem.cc
Code:
temp->link = NULL; // temp node with new data.
temp->link = afterme->link; // afterme current afterme->link = next node
afterme->link = temp;
-----------FULL CODE BELOW--------------
FILENAME = inter.h Interface file
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib> // exit
using namespace std;
struct FriendData // Defition of structor.
{
string friendname;
string phonenumber;
int bday;
int bmonth;
FriendData* link; // link points to next FriendData.
};
class List
{
public:
typedef FriendData* nodeptr; //
List(); // Default Constuctor.
// Sets head = NULL;
void insert(); // Insert a new friend.
// Prompt user for name and if not already on list, insert
// new friend into list by date of birth.
void sortbymonth(); // Search for all birthday by month.
// Prompt for a month entered as a int 1...12 then show
// all friends with a birhtday that month.
void output(ostream& outf); // Write list back to file.
// Clean data and write it back to file,
private:
nodeptr head;
}; // End Class
// NON-MEMBER
int menu();
// Implementation filename = implem.cc
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib> // exit
#include "inter.h"
using namespace std;
void List::insert() // Insert a new friend function.
{
nodeptr templist, temp, afterme;
templist = head;
temp = new FriendData;
afterme = head;
string name = "";
string phone = "123-123-1234";
int bday = 0;
int bmonth = 0;
int dontdo = 1; // Used as control stop when name already on list
// or when new name already inserted.
cout << "\n";
cout << "ENTER NEW FRIEND NAME: ";
cin.ignore();
getline(cin,name);
cout << "\n";
cout << "ENTER NEW FRIEND PHONE NUMBER: ";
cin.ignore();
getline(cin,phone);
cout << "\n";
cout << "ENTER NEW FRIEND BIRTH DAY: ";
cin >> bday;
cout << "\n";
cout << "ENTER NEW FRIEND BIRTH MONTH: ";
cin >> bmonth;
temp->friendname = name;
temp->phonenumber = phone;
temp->bday = bday;
temp->bmonth = bmonth;
templist = head; // Reset list.
if (bmonth < templist->bmonth)
{temp->link = NULL;
temp->link = afterme->link;
afterme->link = temp;
cout << "\n";
cout << "NEW Friend TEST = \"" << name << "\" ADDED.";
cout << "\n";
cout << "Press ENTER to continue.";
cin.ignore(1);
cin.get();
cout << "\n\n"; // Wait for user to press enter.
dontdo = 0;
} //
Last edited by a moderator: