Mọi người ơi giúp mình chạy ctirnh này với, mình cho người dùng ấn Y or “y” thì nó crash luôn, không thể nào quay lại lúc enter your choice.
typedef struct Student{
char name[20];
char country[20];
int birthday_year ;
struct mark{
float diemToan;
float diemHoa;
float diemLy;
}Mark3mon;
} Student;
void Input(Student *students, int numberofstudents){
int i =0 ;
for(i = 0;i<numberofstudents; i++){
Student *currentstudent = students+i;
printf("\nNhap ten: ");fflush(stdin);gets(currentstudent->name);
printf("\nNhap que huong: ");fflush(stdin);gets(currentstudent->country);
do{
printf("\nNhap nam sinh: "); scanf("%d",¤tstudent->birthday_year);fflush(stdin);
}while((CURRENT_YEAR-currentstudent->birthday_year)>20);
do{
printf("\nNhap diem Toan: ");scanf("%f",¤tstudent->Mark3mon.diemToan);fflush(stdin);
}while(currentstudent->Mark3mon.diemToan>10&¤tstudent->Mark3mon.diemToan<0);
do{
printf("\nNhap diem Ly: ");scanf("%f",¤tstudent->Mark3mon.diemLy);fflush(stdin);
}while(currentstudent->Mark3mon.diemLy>10&¤tstudent->Mark3mon.diemLy<0);
do{
printf("\nNhap diem Hoa: ");scanf("%f",¤tstudent->Mark3mon.diemHoa);fflush(stdin);
}while(currentstudent->Mark3mon.diemHoa>10&¤tstudent->Mark3mon.diemHoa<0);
}
}
void Output(Student *students, int numberofstudents){
int i = 0;
printf("\n=================================================================================================");
printf("\n|Ten hoc sinh |Que Huong |Nam Sinh |Diem Toan |Diem Ly |Diem Hoa |");
for(i = 0;i<numberofstudents;i++){
Student *currentstudent = students+i;
printf("\n=================================================================================================");
printf("\n|%15s|%15s|%15d|%15.2f|%15.2f|%15.2f|",
currentstudent->name,
currentstudent->country,
currentstudent->birthday_year,
currentstudent->Mark3mon.diemToan,
currentstudent->Mark3mon.diemLy,
currentstudent->Mark3mon.diemHoa
);
}
}
void Sort(Student *students, int numberofstudents){
int i =0;
int j =0;
Student temp;
Student *si = students+i;
Student *sj = students+j;
float TotalI = si->Mark3mon.diemLy+si->Mark3mon.diemLy+si->Mark3mon.diemHoa;
float TotalJ = sj->Mark3mon.diemLy+sj->Mark3mon.diemLy+sj->Mark3mon.diemHoa;
for(i=0;i<numberofstudents-1;i++){
for(j = i+1;numberofstudents;j++){
if (TotalI<TotalJ)
temp = *si;
*si = *sj;
*sj = temp;
}
}
}
int main (){
Student *students = NULL;
int numberofstudents;
char choice;
printf("---------------------Student Management---------------------\n");
printf("------------------------------------------------------------\n");
printf("1. Input |2. Sort |3. Analyze|4. Find|5. Save|6. Open|7.Exit ");
do{
printf("\nEnter your choice: ");
choice = getchar();
fflush(stdin);
while(choice == '\n'){
choice = getchar();
}
switch(choice){
case '1':
printf("\nNhap so luong sinh vien :"); scanf("%d",&numberofstudents); fflush(stdin);
students = (Student*)malloc(numberofstudents*sizeof (Student*));
Input(students,numberofstudents);
Output(students,numberofstudents);
break;
case '2':
if(students == NULL&&numberofstudents >0)
Sort(students,numberofstudents);
Output(students,numberofstudents);
break;
case '3':
printf("You chose to analyze\n");
break;
case '4':
printf("You chose to find\n");
break;
case '5':
printf("You chose to save\n");
break;
case '6':
printf("You chose to open\n");
break;
default:
break;
}
printf("\nDo you want to continue the program?\n");
printf("Co. (bam phim y, Y) \n");
printf("Khong (bam phim n, N)\n");
printf("Clear man hinh ! (bam c, C)\n");
printf("Your choice is: ");
fflush(stdin);
choice = getchar();
if(choice == 'c'||'C'){
system("cls");
}
}while(choice !='n'||choice !='N');
}