There's more than just one error actually.
Firstly
CODE
#define max 50;
should be
CODE
#define max 50
No semi-colon is used to terminate a preprocessor directive. Remember the #define directive just replaces max with the number 50. Its not a variable. If you use a semicolon, max would be replaced by 50; so your code declaring the info variable of type employee would become:
CODE
struct employee info[50;]
which would produce a compiler error.
Secondly, as you thought correctly, your declaration of temp is wrong.
CODE
temp=info[i].age; //here the compiler shows an error
info[i].age=info[j].age;
info[j].age=temp;
Yo have declared temp as type employee. However ince you're usin temp to store info[i].age (and not the entire structure), temp should be an int.
so your code should change to:
CODE
int i,j,n,temp;
struct employee info[max];
Next, you don't put a semicolon after the for loop. In doing so the compiler will assume that a null statement follows the loop. Also (assuming its bubble sort you are using) there are a few corrections to be made in the sorting algorithm you have used.
With all those changes your code should be.
CODE
#include<stdio.h>
#include<stdlib.h>
#define max 50
struct employee
{
char name[20];
int age;
};
int main()
{
int i,j,n,temp;
struct employee info[max];
printf("\nHow many employee will be enter: ");
scanf("%d",&n);
for(i=0;i<n;++i)
{
printf("\nPlease enter #%d employee's name: ",i+1);
fflush(stdin);
gets(info[i].name);
printf("\nPlease enter #%d employee's age: ",i+1);
scanf("%d",&info[i].age);
}
for(i=1; i<n; i++)
{
for(j=0; j<n-i;j++)
{
if(info[j].age>info[j+1].age)
{
temp=info[j].age;
info[j].age=info[j+1].age;
info[j+1].age=temp;
}
}
}
for(i=0;i<n;i++)
{
printf("\n%s %d",info[i].name,info[i].age);
}
system("pause");
return 0;
}
This post has been edited by Louisda16th: 17 Jul, 2007 - 07:02 PM