我在我的函数 main 中收到错误,clrscr();
但我认为我必须在使用时清除fflush(stdin);
?
我觉得我在这里遗漏了一些简单的东西,但如果有人能像我一样摆脱一些,我将不胜感激!
#include<stdio.h>
#include<conio.h>
#include<string.h>
struct person
{
char name[10];
int age;
};
typedef struct person NAME;
NAME stud[10], temp[10];
void main()
{
int no,i;
void sort(int N); /* Function declaration */
clrscr();
fflush(stdin);
printf("Enter the number of students in the list\n");
scanf("%d",&no);
for(i = 0; i < no; i++)
{
printf("\nEnter the name of person %d : ", i+1);
fflush(stdin);
gets(stud[i].name);
printf("Enter the age of %d : ", i+1);
scanf("%d",&stud[i].age);
temp[i] = stud[i];
}
printf("\n*****************************\n");
printf (" Names before sorting \n");
/* Print the list of names before sorting */
for(i=0;i<no;i++)
{
printf("%-10s\t%3d\n",temp[i].name,temp[i].age);
}
sort(no); /* Function call */
printf("\n*****************************\n");
printf (" Names after sorting \n");
printf("\n*****************************\n");
/* Display the sorted names */
for(i=0;i<no;i++)
{
printf("%-10s\t%3d\n",stud[i].name,stud[i].age);
}
printf("\n*****************************\n");
} /* End of main() */
/* Function to sort the given names */
void sort(int N)
{
int i,j;
NAME temp;
for(i = 0; i < N-1;i++)
{
for(j = i+1; j < N; j++)
{
if(strcmp(stud[i].name,stud[j].name) > 0 )
{
temp = stud[i];
stud[i] = stud[j];
stud[j] = temp;
}
}
}
} /* end of sort() */