Sorting structures

This is a discussion on Sorting structures within the C++ Programming forums, part of the General Programming Boards category; Hi there, am having problems sorting the structures by name(alphabeticaly). Below is the code for this.When i run this it ...

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    23

    Sorting structures

    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;
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this.

    #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);


    int 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;
    cin.ignore(100,'\n');

    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(1);

    getch();
    return 0;
    }
    // **************************************************
    friends fill_struct()
    {
    friends person;
    cout<<"Whats this persons name?\n";
    cin.getline(person.name,25);
    cout<<"Whats this persons phone number?\n";
    cin>>person.phone;
    cin.ignore(100,'\n');
    cout<<"Whats this person's address?\n";
    cin.getline(person.address,45);

    return(person);
    }
    // **************************************************
    void phone_list(friends people[20],int ctr)
    {
    char tmp[25];
    char taddress[45];
    long double tphone;
    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);
    strcpy(tmp,people[o].address);
    strcpy(people[o].address,people[n].address);
    strcpy(people[n].address,tmp);
    tphone = people[o].phone;
    people[o].phone = people[n].phone;
    people[n].phone = tphone;
    }
    }
    }


    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;
    }

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    23
    swoopy thanxs so much . I appreciate your help .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-11-2009, 12:27 PM
  2. Problem with arrays structures sorting.
    By pitifulworm in forum C Programming
    Replies: 42
    Last Post: 02-09-2009, 12:31 PM
  3. Array of Structures: Sorting
    By Drainy in forum C Programming
    Replies: 3
    Last Post: 04-13-2005, 10:55 AM
  4. Sorting an array of structures with sort()
    By rmullen3 in forum C++ Programming
    Replies: 3
    Last Post: 01-03-2003, 03:02 PM
  5. Methods for Sorting Structures by Element...
    By Sebastiani in forum C Programming
    Replies: 9
    Last Post: 09-14-2001, 01:59 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21