//ptrswap2.cpp
/* Description: The following main program reads in two records of information.
	Each record contains an ascii name and original order. The main
	coordinates allocating, printing, ordering and deallocating the
	data. The five functions actually implement the functionality.
*/
#include <stdlib.h>
#include <iostream.h>
#include <string.h>
struct Name	{
	char *name;		//holds onto name typed in
	int originalOrder;	//holds order name typed in - not sorted order
};
	//allocates memory off heap for structure and inits order value
struct Name* allocateName(int order)	{
	Name *name= new Name;
	name->name=0;
	name->originalOrder=order;
	return name;
}
	//prompts for name and allocates memory for structure to hold onto it
void getName(struct Name* name)	{
	char buffer[80];
	name->name=0;
	cout << "enter name:";
	cin >> buffer;
	name->name = new char[strlen(buffer)+1];
	strcpy(name->name,buffer);
}
	//prints names with their original entered order
void printNames(struct Name* name1, struct Name* name2)	{
	cout << "#" << name1->originalOrder << ", " << name1->name << endl;
	cout << "#" << name2->originalOrder << ", " << name2->name << endl;
}
	//swaps the pointers to structs based on ascii string compare
void orderNames(struct Name** name1, struct Name **name2)	{
	if (strcmp((*name1)->name,(*name2)->name)>0)	{
		struct Name* temp = (*name2);
		(*name2) = (*name1);
		(*name1) = temp;
	}
}
	//deallocates all memory allocated in structure
void deallocateName(struct Name* name)	{
	delete [] name->name;
	delete name;
}
main()	{
		//allocate and initialize the structures
	Name* name1 = allocateName(1);
	Name* name2 = allocateName(2);
		//prompt and read in the names
	getName(name1);
	getName(name2);
		//print original order
	printNames(name1,name2);
		//order the names
	orderNames(&name1,&name2);
		//print the names in new order
	printNames(name1,name2);
		//deallocate the names
	deallocateName(name1);
	deallocateName(name2);
	return 0;
}
