#include <stdio.h>
#include <string.h>

int i,k,type;
char buffer[100],l,line[50];

struct  student
{
    char name[20];
    int age;
    int id;
};
struct student st[2],temp,*sp;

int sort(struct student *p)
{
    *sp=st[0];
    p->age;
for(i=0; i<l; i++)
    {
        if(st[i].age<st[i+1].age)
        break;
         else
        if(st[i].age>st[i+1].age)
        {
            temp=st[i];
            st[i]=st[i+1];
            st[i+1]=temp;
        }break;
    }   
    printf("\t\tsorted student information\n--------------------------\n");
    printf("%d",st[i].age);
    return(0);
}

int main()
{
    for(i=0;i<3;i++)
    { 
        printf("please enter your name:\n");
        fgets(st[i].name,sizeof(st[i].name),stdin);
        printf("please enter your age:\n");
        fgets(buffer,sizeof(buffer),stdin);
        sscanf(buffer,"%d",&st[i].age); 
        printf("please enter your id:\n");
        fgets(buffer,sizeof(buffer),stdin);
        sscanf(buffer,"%d",&st[i].id);
    }   
        while(1)
        {
            printf("Please enter the # for the program you want to run:\n");
            fgets(line,sizeof(line),stdin);
            sscanf(line,"%d",&type);
            if(type==5)
            break;
            switch(type)
            {

            case 1:/*sort by name */
                sort(sp);
                break;
            case 2:/* sort by age  */
                sort(sp);
                break;
            case 3:/* sort by id   */
                sort(sp);
                break;
            case 4:
                printf("1-sort by name\n2-sort by age\n3-sort by id\n4-help\n5-Quit\n");
                continue;

            default:
                printf("enter 4 for help\n");
                continue;
            }
        }                           
return(0);    
}

ok, so this is kinda a repost, but with different problems. whats going on is the user needs to input students information(name, age, and id) the there is a switch case so that the data entered can be sorted by either name, age or id. right now its only set up to sort by age, and doesnt work, i get a segmentation error in the unix editor. So, i was wondering what i did wrong(im sure there are more than one error) and if i need to write three different functions for each sort.

Where is the problem occuring? Have you done any debugging at all? Do your records that you attempt to sort even exist?

>i was wondering what i did wrong
Start by getting the sorting algorithm correct with simple test cases. All of the framework is only going to make debugging harder. But I can tell you right now that your algorithm is wrong. Check my sorting routines entry in the Code Snippets section for ideas.

>if i need to write three different functions for each sort
For age and id you could get away with using a single sort function, but for strings you'll either need another function that handles strings properly, or pass a comparison function as an argument like qsort does.

i really have no idea what u mean by debugging, i dont know where the problem is occuring b/c all it says is segmentation fault, and i dont really know what that is, where do i get records of sorting?

k, thanks!

Where is the problem occuring? Have you done any debugging at all? Do your records that you attempt to sort even exist?

what is a segmentation error?

Ususally it means you referenced a pointer that is NULL or uninitialized. In your case, 'sp' appears to be an uninitialized pointer.

If you don't know what debugging is you should look up about that first and start to practice it...

Be a part of the DaniWeb community

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