#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.