Hi there, am having problems sorting the structures by name(alphabeticaly). Below is the code for this.When i run this it prints
all the structure members names the same in the phone_list function (Please ignore the full_list function haven't finished that yet ).The code is very messy i know, but wanted to fix the sorting thing first and then go on and polish up the whole thing.
Thanks for your help.
[code]
---------------------------------------------------------------------------------
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <dos.h>
#include <string.h>
struct friends
{
char *name[25];
long double phone;
char address[45];
};
struct friends fill_struct();
void phone_list(friends people[20],int ctr);
void full_list(friends people[20],int ctr);
void main()
{
friends people[20];
int ans;
int ctr=0;
clrscr();
do
{
cout<<"\n\n1) Enter new friend\n";
cout<<"2) View Phone listing\n";
cout<<"3) View phone/address listing\n";
cout<<"4) Exit";
cout<<"\nWhat is your choice?";
cin>>ans;
switch (ans)
{
case (1) :{people[ctr]=fill_struct();
ctr++;
break;
}
case (2) :{phone_list(people,ctr);
break;
}
case (3) :{full_list(people,ctr);
break;
}
case (4) :{exit(0);
}
default :{cout<<"Incorect Entry, try again\n";
delay(2000);
break;
}
}
}while((ans!=1) ||(ans!=2) ||(ans!=3) ||(ans!=4));
getch();
return;
}
//************************************************** *
friends fill_struct()
{
friends person;
cout<<"Whats this persons name?\n";
gets(*(person.name));
cout<<"Whats this persons phone number?\n";
cin>>person.phone;
cout<<"Whats this person's address?\n";
gets(person.address);
return(person);
}
//************************************************** *
void phone_list(friends people[20],int ctr)
{
char tmp[25];
for (int o=0; o<(ctr-1); o++)
{
for(int n=(o+1); n<ctr; n++)
{
int ptr=strcmp(*people[o].name,*people[n].name);
if (ptr>0)
{
strcpy(tmp,*people[o].name);
strcpy(*people[o].name,*people[n].name);
strcpy(*people[n].name,tmp);
}
}
}
cout<<"****FRIENDs****\n";
for(int i=0; i<ctr; i++)
{
cout<<"\n\nNAME: "<<*people[i].name;
cout<<"\n\nPHONE #: "<<people[i].phone;
}
return;
}
//************************************************** *
void full_list(friends people[20],int ctr)
{
for(int i=0; i<ctr; i++)
{
cout<<"****FRIEND****\n";
cout<<"\n\nNAME: "<<*people[i].name;
cout<<"\n\nPHONE #: "<<people[i].phone;
cout<<"\n\nADDRESS: "<<people[i].address;
}
return;
}