Hi please may u have a read and see why my third sort on age is not working correctly ........ id and wage work fine but for some reaason age is malfunctioning thanks greatly !
#include<stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct surv_list SLIST;
struct surv_list {
int id;
int wage;
int age;
int sex;
};
int sort_id(const void *v1, const void *v2) {
SLIST *a1=(SLIST *)v1;
SLIST *a2=(SLIST *)v2;
return (a1->id - a2->id);
}
int sort_wage(const void *v1, const void *v2) {
SLIST *a1=(SLIST *)v1;
SLIST *a2=(SLIST *)v2;
return (a1->id - a2->wage);
}
int sort_age(const void *v1, const void *v2) {
SLIST *a1=(SLIST *)v1;
SLIST *a2=(SLIST *)v2;
return (a1->id - a2->age);
}
int main()
{
int t;
char sort;
int /* PersonID,Wage,Sex,Age, */ n, i;
SLIST a[10], *aptr=a;
printf("please enter the amount of people you will use \n");
scanf("%d",&n);
printf("\n=%d\n\n", n);
for (i=0; i<n; i++)
{
printf("please enter the Person ID (person %d)\n", i);
scanf("%d",&a[i].id);
printf("please enter the Wage (person %d)\n", i);
scanf("%d",&a[i].wage);
printf("please enter the Age(person %d)\n", i);
scanf("%d",&a[i].age);
printf("please enter the Sex use 1 for male and 0 for female(person %d)\n", i);
scanf("%d",&a[i].sex);
}
printf("\t Survey Data:\n");
printf("\t~~~~~~~~~~~~~~~~~~~\n");
for (i=0; i<n; i++)
{
printf("Person %d\t", i);
printf("ID: %d\t", a[i].id);
printf("Wage: %d\t", a[i].wage);
printf("Sex: %d\t", a[i].sex);
printf("Age: %d\t\n", a[i].age);
}
t=1;
while (t==1)
{
printf("\n\rHow do you want the data to be sorted ?");
printf("\n\rAscending by Person ID,type a");
printf("\n\rAscending by Wage, type b");
printf("\n\rAscending by Age, type c");
printf("\n\rOnly females, print d");
printf("\n\rOnly males, print e\n\r");
scanf("\n%c",&sort);
switch(sort) {
case 'a':
case 'A':
qsort(a, n, sizeof(SLIST), sort_id);
}
switch(sort) {
case 'b':
case 'B':
qsort(a, n, sizeof(SLIST), sort_wage);
}
switch(sort) {
case 'c':
case 'C':
qsort(a, n, sizeof(SLIST), sort_age);
}
printf("\t Survey Data:\n");
printf("\t~~~~~~~~~~~~~~~~~~~\n");
for (i=0; i<n; i++)
{
printf("Person %d\t", i);
printf("ID: %d\t", a[i].id);
printf("Wage: %d\t", a[i].wage);
printf("Sex: %d\t", a[i].sex);
printf("Age: %d\t\n", a[i].age);
}
printf("Would you like to sort the data again Yes = 1 No = 0 \n");
scanf("%d",&t);
}
}