plz friend solve this code for me I dont know what is the wrong with it. it is in c++ programing language.
#include <stdio.h>
#include <string.h>
#define size 20
struct data
{
int age;
int year;
char course[6];
char name[20];
} students[20];
struct temporary //changed to struct?
{
int age;
int year;
char course[6];
char name[20];
} holder;
int main()
{
int x,limit;
int input,inputdata,showdata, sort_asc_by_age;
do
{
//clrscr(); non portable avoid if possible
puts("1..insert data");
puts("2..show data");
puts("3..show data in ascending order by age");
puts("4..show data in ascending order by year");
puts("5..show data in descending order by age");
puts("6..show data in descending order by year");
puts("9..exit");
printf("input choice:"); scanf("%d",&input);
switch(input)
{
case 1:
inputdata();
break;
case 2:
showdata();
break;
case 3:
sort_asc_by_age(); //added this case
break; //added this case
}
}while(input!=9);
getchar(); //getchar better than getch()
//getch is non-portable
return 0;
}
inputdata()
int limit,x;
printf("Size of structure to make? (1-20)\t");
scanf("%d",&limit);
if(limit<1 || limit>size)
{
printf("Input Invalid!");
}
else
{
limit=limit-1;
printf("\n\n\nAge Year Course Name\n");
for(x=0; x<=limit; x++)
{
printf("Age:");
scanf("%d",&students[x].age);
printf("Year:");
scanf("%d",&students[x].year);
printf("Course:");
scanf("%s",students[x].course); //gets = bad
printf("Name:");
scanf("%s",students[x].name); //gets = bad
//although scanf is a lesser evil
//for better a better input tutorial in c
//check out dave's tutorial
printf("\nNext entry\n");
}
}
showdata()
{
int limit;
int x;
printf("This are the items you inputted!");
printf("\nAge Year level Course Name\n");
for(x=0;x<size;x++)
{
printf("%d",students[x].age);
printf("\t%d",students[x].year);//changed to %d (it is an integer!)
printf("\t%s",students[x].course);
printf("\t%s\n",students[x].name);
}
getchar(); //getchar better than getch()
}
sort_asc_by_age()
{
int x,y;
for(x = 0; x < size; x++)
for(y = 0; y < size; y++)
if(students[y].age > students[y+1].age)
{
holder.age=students[y+1].age;
holder.year=students[y+1].year;
strcpy(holder.course,students[y+1].course); //changed string copy
strcpy(holder.name,students[y+1].name);// changed
students[y+1].age=students[y].age;
students[y+1].year=students[y].year;
strcpy(students[y+1].name,students[y].name);//changed
strcpy(students[y+1].course,students[y].course);//changed
students[y].age=holder.age;
students[y].year=holder.year;
strcpy(students[y].name,holder.name); //changed
strcpy(students[y].course,holder.course);//changed
}
for(x=0;x<size;x++)
{
printf("%d",students[x].age);
printf("\t%d",students[x].year); //%d
printf("\t%s",students[x].course);
printf("\t%s\n",students[x].name);
}
getch();
}
sort_asc_by_year()
{
int x,y;
for(x = 0; x < size; x++)
for(y = 0; y < size-1; y++)
if(students[y].year > students[y+1].age)
{
holder.age=students[y+1].age;
holder.year=students[y+1].year;
strcpy(holder.course,students[y+1].course); //changed
strcpy(holder.name,students[y+1].name);//changed
students[y+1].age=students[y].age;
students[y+1].year=students[y].year;
strcpy(students[y+1].name,students[y].name);//changed
strcpy(students[y+1].course,students[y].course);//changed
students[y].age=holder.age;
students[y].year=holder.year;
strcpy(students[y].name,holder.name); //changed
strcpy(students[y].course,holder.course);//changed
}
for(x=0;x<size;x++)
{
printf("%d",students[x].age);
printf("\t%d",students[x].year); //%d
printf("\t%s",students[x].course);
printf("\t%s\n",students[x].name);
}
getchar(); //getchar
}
sort__desc_by_age()
{
int x,y;
for(x = 0; x < size; x++)
for(y = 0; y < size; y++)
if(students[y].age < students[y+1].age)
{
holder.age=students[y+1].age;
holder.year=students[y+1].year;
strcpy(holder.course,students[y+1].course); //changed
strcpy(holder.name,students[y+1].name);//changed
students[y+1].age=students[y].age;
students[y+1].year=students[y].year;
strcpy(students[y+1].name,students[y].name);//changed
strcpy(students[y+1].course,students[y].course);//changed
students[y].age=holder.age;
students[y].year=holder.year;
strcpy(students[y].name,holder.name); //changed
strcpy(students[y].course,holder.course);//changed
}
for(x=0;x<size;x++)
{
printf("%d",students[x].age);
printf("\t%d",students[x].year); //%d
printf("\t%s",students[x].course);
printf("\t%s\n",students[x].name);
}
getch();
}
sort_desc_by_year()
{
int x,y;
for(x = 0; x < size; x++)
for(y = 0; y < size-1; y++)
if(students[y].year < students[y+1].age)
{
holder.age=students[y+1].age;
holder.year=students[y+1].year;
strcpy(holder.course,students[y+1].course); //changed
strcpy(holder.name,students[y+1].name);//changed
students[y+1].age=students[y].age;
students[y+1].year=students[y].year;
strcpy(students[y+1].name,students[y].name);//changed
strcpy(students[y+1].course,students[y].course);//changed
students[y].age=holder.age;
students[y].year=holder.year;
strcpy(students[y].name,holder.name); //changed
strcpy(students[y].course,holder.course);//changed
}
for(x=0;x<size;x++)
{
printf("%d",students[x].age);
printf("\t%d",students[x].year); //%d
printf("\t%s",students[x].course);
printf("\t%s\n",students[x].name);
}
getchar(); //getchar()
}