Hi guys, I am currently doing my assignment. It requires me to create a program that will be able to create an address book using File.So far, I managed to display all the contacts, search the contacts and delete the contacts. But , I m not able to sort the name of the ones in my address book in alphabetical order..

I have some idea, and I know I must use array, but HOW??
Anyone, please give your idea... Thanks in advance..

Recommended Answers

All 9 Replies

1) GOOGLE it

2) use std::sort function.

If u don mind, could u just give me an example of the usage of such function... I haven't heard of that function before..

int num[10] = {5,3,-1,2,5,12,42,-35,-2,0 };
std::sort(num,num+10);

Try this link : google

Will this work for C??
I see that the link u gave is for C++...

By the way, this is the code I m working on... I wish to store the "name" into the array, but i don know what code should I use...
Please refer the colored letters..There is where I wish to put the code...

void arrangeName(void)
{
    FILE*arrange;
    int line=0,x,result;
    char name[100],sname,arr[100],NwName[100];

    arrange=fopen("AddressBook.txt","r");

    result=fscanf(arrange,"%s",name);

    for(x=0;result==1;x++)
    {
        [COLOR="Red"]HERE IT IS[/COLOR]
                gets(name)=arr[x];
        while(fscanf(arrange,"%c",&sname)!=0)
        {
            if(sname=='\n')
                line++;
                if(line==7)
                    EOF;
        }
        result=fscanf(arrange,"%s",name);
    }

    printf("%s",arr[0]);

When you post code, use code tag's like so:
[code=C]
<code here>
[/code]

qsort() is what your looking for.

Try it.

qsort() what your looking for.

But be careful with the example from that page. Subtracting ints like that without any kind of underflow test has some risk. The safe way is easier to understand too:

#include <stdio.h>
#include <stdlib.h>

int Compare(void const* a, void const* b)
{
    int const* first = a;
    int const* second = b;

    if (*first == *second) return 0;
    else if (*first < *second) return -1;
    else return 1;
}

int main()
{
    int a[] = {0,4,6,2,8,7,1,6,8,4,3,2,8,5,9};
    size_t const sz = sizeof a / sizeof *a;
    size_t x;

    for (x = 0; x < sz; ++x) printf("%2d%s", a[x], x < sz - 1 ? "" : "\n");
    qsort(a, sz, sizeof *a, Compare);
    for (x = 0; x < sz; ++x) printf("%2d%s", a[x], x < sz - 1 ? "" : "\n");

    return 0;
}
commented: Nice +36

Thanks for all you guys help!!.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.