Code:
#include <iostream>
#include <string>
#include <fstream>
#include "SortedList.h"
using namespace std;
const MAX_LENGTH = 100;
struct me
{
string lName;
string fName;
string mName;
string title;
string companyName;
string streetAddress;
string city;
string state;
string zipCode;
string phoneNumber;
string faxNumber;
string emailAddress;
me *link;
};
int main()
{
SortedList list;
// Where do I go from here
me *Head = NULL;
me *Current;
me *Next;
me *Previous;
ofstream fileOutput;
fileOutput.open("contacts.dat");
Current = Head;
Previous = Current;
Next = new me;
char another;
int count = 0;
cout << "Enter last name: ";
getline(cin, Next->lName);
cout << "Enter first name: ";
getline(cin, Next->fName);
cout << "Enter middle name: ";
getline(cin, Next->mName);
cout << "Enter title: ";
getline(cin, Next->title);
cout << "Enter company name: ";
getline(cin, Next->companyName);
cout << "Enter street address: ";
getline(cin, Next->streetAddress);
cout << "Enter city: ";
getline(cin, Next->city);
cout << "Enter state: ";
getline(cin, Next->state);
cout << "Enter zip code: ";
getline(cin, Next->zipCode);
cout << "Enter phone number: ";
getline(cin, Next->phoneNumber);
cout << "Enter fax number: ";
getline(cin, Next->faxNumber);
cout << "Enter e-mail address: ";
getline(cin, Next->emailAddress);
count++;
cin.ignore(MAX_LENGTH, '\n');
if ( Head == NULL )
{
Head = Next;
Current = Head;
Current->link = NULL;
Previous = Current;
}
else
{
if ( Next->lName < Current->lName )
{
Next->link = Head;
Head = Next;
}
else
{
Current = Head;
Previous = Head;
while ( Current != NULL )
{
if ( Next->lName < Current->lName)
{
Next->link = Current;
Previous->link = Next;
break;
}
else
{
Previous = Current;
Current = Current->link;
}
}
if ( Current == NULL )
{
Previous->link = Next;
}
}
}
cout << "Would you like to enter another contact(Y/N): ";
cin >> another;
while ( another == 'Y' || another == 'y' )
{
cin.ignore(MAX_LENGTH, '\n');
Current = Head;
Previous = Current;
Next = new me;
cout << "Enter last name: ";
getline(cin, Next->lName);
cout << "Enter first name: ";
getline(cin, Next->fName);
cout << "Enter middle name: ";
getline(cin, Next->mName);
cout << "Enter title: ";
getline(cin, Next->title);
cout << "Enter company name: ";
getline(cin, Next->companyName);
cout << "Enter street address: ";
getline(cin, Next->streetAddress);
cout << "Enter city: ";
getline(cin, Next->city);
cout << "Enter state: ";
getline(cin, Next->state);
cout << "Enter zip code: ";
getline(cin, Next->zipCode);
cout << "Enter phone number: ";
getline(cin, Next->phoneNumber);
cout << "Enter fax number: ";
getline(cin, Next->faxNumber);
cout << "Enter e-mail address: ";
getline(cin, Next->emailAddress);
count++;
if ( Head == NULL )
{
Head = Next;
Current = Head;
Current->link = NULL;
Previous = Current;
}
else
{
if ( Next->lName < Current->lName )
{
Next->link = Head;
Head = Next;
}
else
{
Current = Head;
Previous = Head;
while ( Current != NULL )
{
if ( Next->lName < Current->lName)
{
Next->link = Current;
Previous->link = Next;
break;
}
else
{
Previous = Current;
Current = Current->link;
}
}
if ( Current == NULL )
{
Previous->link = Next;
}
}
}
cout << "Would you like to enter another contact(Y/N): ";
cin >> another;
}
if ( another == 'N' || another == 'n' )
{
fileOutput << "There are " << count << " contacts in this file." << endl;
Current = Head;
while ( Current != NULL )
{
fileOutput << Current->lName << endl;
fileOutput << Current->fName << endl;
fileOutput << Current->mName << endl;
fileOutput << Current->title << endl;
fileOutput << Current->companyName << endl;
fileOutput << Current->streetAddress << endl;
fileOutput << Current->city << endl;
fileOutput << Current->state << endl;
fileOutput << Current->zipCode << endl;
fileOutput << Current->phoneNumber << endl;
fileOutput << Current->faxNumber << endl;
fileOutput << Current->emailAddress << endl;
Current = Current->link;
}
}
fileOutput.close();
cout << "Data has been written to the file." << endl;
return 0;
}