You have done plenty! Thank you so much, I actually just fixed it i<cnt-2 and j<cnt-1 is the correct way to do it. Ill post the completed code.
Code:
#include <string.h>
#include <stdio.h>
struct student
{
int student_id;
char name[30];
char grade[15];
};
void sort(struct student st[],int cnt,int choice);
int main(void)
{
int i;
int students;
int input;
struct student stu[20];
printf("How many students in the Class?: ");
scanf("%d", &students);
for(i = 0; i < students; i++)
{
printf("Enter name: ");
scanf("%s",stu[i].name);
printf("Enter id: ");
scanf("%d", &stu[i].student_id);
printf("Enter grade: ");
scanf("%s",stu[i].grade);
printf("\n");
}
printf("how do you want to sort the records? [0 - name, 1 - id, 2 - grade]: ");
scanf("%d", &input);
sort(stu,students,input);
for(i = 0; i < students; i++)
{
printf(" %s, ID: %d, has a grade: %s\n\n",
stu[i].name,stu[i].student_id,stu[i].grade);
}
return 0;
}
void sort(struct student st[],int cnt,int choice)
{
int i,j;
struct student temp;
for(i=0;i<cnt-2;i++)
{
for(j=0;j<cnt-1;j++)
{
if(
(choice==0 && strcmp(st[j].name,st[j+1].name)>0)
||
(choice==1 && st[j].student_id>st[j+1].student_id)
||
(choice==2 && st[j].grade>st[j+1].grade)
)
{
temp=st[j];
st[j]=st[j+1];
st[j+1]=temp;
}
}
}
}