« -: Mar 25, 2015, 10:46 »
Здравейте, исползвам метода на мехуечето за да сортирам имена по азбучен ред. Когато стринговете не са в структура се получава, но когато опитам да ги напраяв във структура със други данни ми дава куп грешки.
Ето сорса:
#include <stdio.h>
#include <string.h>
struct students {
char name[80];
int age[80];
} item[100];
int main()
{
char t[100];
int maxstud, j, e;
int choice;
printf("How many students you have?: ");
scanf("%d", &maxstud);
for(j=0; j<maxstud; j++){
printf("Enter Name: ");
scanf("%s", item[j].name);
printf("Enter Age ");
scanf("%d", &item[j].age);
}
printf("Sorting: \n");
printf("1. Sort by name (alhabetical)\n");
printf("2. Sort by age\n");
printf("Enter your choicie: ");
scanf("%d", &choice);
if(choice==1){
for(j=1; j<maxstud-1; ++j)
for(e=maxstud-1; e>=j; --e){
if(strcmp(item[e-1].name, item[e].name) >0){
strcpy(t, item[e-1]);
strcpy(item[e-1], item[e]);
strcpy(item[e], t);
}
}
for(j=0; j<maxstud; j++)
printf("Name: %s \t Age: ", item[j].name, item[j].age);
}
return 0;
}
Ето и грешките от компилатора:
est.c: In function ‘main’:
test.c:23:5: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int (*)[80]’ [-Wformat=]
scanf("%d", &item[j].age);
^
test.c:36:11: error: incompatible type for argument 2 of ‘strcpy’
strcpy(t, item[e-1]);
^
In file included from test.c:2:0:
/usr/include/string.h:129:14: note: expected ‘const char * __restrict__’ but argument is of type ‘struct students’
extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
^
test.c:37:11: error: incompatible type for argument 1 of ‘strcpy’
strcpy(item[e-1], item[e]);
^
In file included from test.c:2:0:
/usr/include/string.h:129:14: note: expected ‘char * __restrict__’ but argument is of type ‘struct students’
extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
^
test.c:37:11: error: incompatible type for argument 2 of ‘strcpy’
strcpy(item[e-1], item[e]);
^
In file included from test.c:2:0:
/usr/include/string.h:129:14: note: expected ‘const char * __restrict__’ but argument is of type ‘struct students’
extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
^
test.c:38:11: error: incompatible type for argument 1 of ‘strcpy’
strcpy(item[e], t);
^
In file included from test.c:2:0:
/usr/include/string.h:129:14: note: expected ‘char * __restrict__’ but argument is of type ‘struct students’
extern char *strcpy (char *__restrict __dest, const char *__restrict __src)