Hello Everybody,
I need help. I have found some problem with my code and I could not seem to make it right. I need to sort data in a text file.
this is my data in the text file
Hitachi Ariff 12/5/08 12.567 2
Motorola Raju 12/5/08 15.658 2
Siemens Daniel 14/5/08 16.747 2
Borsch Matthew 21/5/08 25.536 3
LKT Ali 25/5/08 30.224 4
Motorola Bahrom 26/5/08 9.452 1
Siemens Suman 27/8/08 21.453 2
VDO William 30/8/08 50.252 6
Motorola Rahman 31/8/08 122.134 14
Siemens Edmund 27/8/08 155.145 16
Motorola Esther 31/8/08 67.353 7
Borsch Daniel 23/6/08 87.553 9
Hitachi Matthew 24/7/08 79.646 8
VDO Francis 25/7/08 90.224 10
and this is my code
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define ARRAY_SIZE 20
#define STRUCT_SIZE 100
typedef struct {
char machine[ARRAY_SIZE];
char employee[ARRAY_SIZE];
char date[ARRAY_SIZE];
double test_value;
int qualification_rate;
}factory;
int main()
{
FILE *data_txt;
int loop;
int sort;
int i=0;
int a=0;
int b=0;
int p[ARRAY_SIZE],q[ARRAY_SIZE],r[ARRAY_SIZE],s[ARRAY_SIZE],t[ARRAY_SIZE];
factory factory_info[STRUCT_SIZE]={"","","",0.0,0};
printf("Menu\n");
printf("****\n");
printf("1. Machine\n");
printf("2. Employee\n");
printf("3. Qualification Rate\n");
printf("\nChoose the data you want to sort : ");
scanf_s("%d",&sort);
printf("\n");
data_txt = fopen("data.txt","r");
loop = fscanf(data_txt,"%s" "%s" "%s" "%lf" "%d",factory_info[i].machine,factory_info[i].employee,factory_info[i].date,
&factory_info[i].test_value,&factory_info[i].qualification_rate);
if(data_txt == NULL)
{
printf("The text file is not found\n\n");
}
else
{
while(loop != EOF)
{
loop = fscanf(data_txt,"%s" "%s" "%s" "%lf" "%d",factory_info[i].machine,factory_info[i].employee,factory_info[i].date,
&factory_info[i].test_value,&factory_info[i].qualification_rate);
if(sort == 1)
{
for(a=0;a<=STRUCT_SIZE-2;a++)
{
for(b=0;b<=STRUCT_SIZE-1;b++)
{
if(factory_info[a].machine<factory_info[b].machine)
{
strcpy(p,factory_info[a].machine);
strcpy(factory_info[a].machine,factory_info[b].machine);
strcpy(factory_info[b].machine,p);
strcpy(q,factory_info[a].employee);
strcpy(factory_info[a].employee,factory_info[b].employee);
strcpy(factory_info[b].employee,q);
strcpy(r,factory_info[a].date);
strcpy(factory_info[a].date,factory_info[b].date);
strcpy(factory_info[b].date,r);
strcpy(s,factory_info[a].test_value);
strcpy(factory_info[a].test_value,factory_info[b].test_value);
strcpy(factory_info[b].test_value,s);
strcpy(t,factory_info[a].qualification_rate);
strcpy(factory_info[a].qualification_rate,factory_info[b].qualification_rate);
strcpy(factory_info[b].qualification_rate,t);
}
}
}
}
else if(sort == 2)
{
for(a=0;a<=STRUCT_SIZE-2;a++)
{
for(b=0;b<=STRUCT_SIZE-1;b++)
{
if(factory_info[a].employee<factory_info[b].employee)
{
strcpy(p,factory_info[a].machine);
strcpy(factory_info[a].machine,factory_info[b].machine);
strcpy(factory_info[b].machine,p);
strcpy(q,factory_info[a].employee);
strcpy(factory_info[a].employee,factory_info[b].employee);
strcpy(factory_info[b].employee,q);
strcpy(r,factory_info[a].date);
strcpy(factory_info[a].date,factory_info[b].date);
strcpy(factory_info[b].date,r);
strcpy(s,factory_info[a].test_value);
strcpy(factory_info[a].test_value,factory_info[b].test_value);
strcpy(factory_info[b].test_value,s);
strcpy(t,factory_info[a].qualification_rate);
strcpy(factory_info[a].qualification_rate,factory_info[b].qualification_rate);
strcpy(factory_info[b].qualification_rate,t);
}
}
}
}
else if(sort == 3)
{
for(a=0;a<=STRUCT_SIZE-2;a++)
{
for(b=0;b<=STRUCT_SIZE-1;b++)
{
if(factory_info[a].qualification_rate<factory_info[b].qualification_rate)
{
strcpy(p,factory_info[a].machine);
strcpy(factory_info[a].machine,factory_info[b].machine);
strcpy(factory_info[b].machine,p);
strcpy(q,factory_info[a].employee);
strcpy(factory_info[a].employee,factory_info[b].employee);
strcpy(factory_info[b].employee,q);
strcpy(r,factory_info[a].date);
strcpy(factory_info[a].date,factory_info[b].date);
strcpy(factory_info[b].date,r);
strcpy(s,factory_info[a].test_value);
strcpy(factory_info[a].test_value,factory_info[b].test_value);
strcpy(factory_info[b].test_value,s);
strcpy(t,factory_info[a].qualification_rate);
strcpy(factory_info[a].qualification_rate,factory_info[b].qualification_rate);
strcpy(factory_info[b].qualification_rate,t);
}
}
}
}
else
{
printf("This sort does not exist!!!\n\n");
}
for(i=0;i<=STRUCT_SIZE;i++)
{
printf("%s %s %s %g %d\n",factory_info[i].machine,factory_info[i].employee,factory_info[i].date,
factory_info[i].test_value,factory_info[i].qualification_rate);
}
}
}
printf("\n");
fclose(data_txt);
return 0;
}
Please help by pointing out my mistakes and help me to solve it... Really would appreciate any help i can get....