@sir darkhero: Bro, gigamit nako mostly imong code but I changed a few details, like making some parts into swith() statement, etc.. Anyways, thank you kaayo sa help!
Here's my final code:
Code:
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#define FALSE 0
typedef struct
{
char fname[40];
char lname[40];
char mi;
}NAMETYPE;
typedef struct
{
NAMETYPE name;
long int id;
char course[20];
int year;
float gpa;
}STUDREC;
struct node
{
STUDREC student;
struct node *next;
};
typedef struct node nd;
void DisplayMenu(void);
void DeleteNode(nd *);
void GetStudRec(nd *);
void DisplayAllStudentRec(nd *);
void Search(nd *);
void DisplayTopStudent(nd *);
void DisplayStudRec(STUDREC);
void Pause(void);
int main(void)
{
float x = 0;
float *y = &x;
x = *y;
clrscr();
DisplayMenu();
return 0;
}
void DisplayMenu(void)
{
nd *record;
int done = FALSE;
char input;
record = (nd *) malloc(sizeof(nd));
record->next = NULL;
while (!done)
{
printf("----------- MAIN MENU ------------\n\n");
printf("(1) ADD STUDENT RECORD\n");
printf("(2) SEARCH STUDENT RECORD\n");
printf("(3) DISPLAY ALL STUDENT RECORDS\n");
printf("(4) DISPLAY TOP STUDENT\n");
printf("(5) EXIT\n\n");
input = 0;
while (!(input >= '1' && input <= '5'))
{
printf("Enter desired operation (1-5): ");
scanf("%c", &input);
getchar();
if (!(input >= '1' && input <= '5'))
printf("INVALID OPERATION. PLEASE TRY AGAIN.\n\n");
}
printf("\n");
switch(input)
{
case '1':
printf("------- ADD STUDENT RECORD -------\n\n");
GetStudRec(record);
break;
case '2':
printf("----- SEARCH STUDENT RECORD ------\n\n");
Search(record);
break;
case '3':
printf("-- DISPLAY ALL STUDENT RECORDS ---\n\n");
DisplayAllStudentRec(record);
break;
case '4':
printf("------- DISPLAY TOP STUDENT ------\n\n");
DisplayTopStudent(record);
break;
case '5':
printf("GOODBYE!\n");
done = !FALSE;
Pause();
break;
}
printf("\n");
}
while (record->next != NULL)
{
nd *pointer;
pointer = (nd *)malloc(sizeof(nd));
pointer = record;
free(record);
record = pointer->next;
}
}
void GetStudRec(nd *record)
{
int index = 1;
STUDREC student;
while (record->next != NULL)
{
index++;
record = record->next;
}
printf("FIRST NAME of student %d: ", index);
scanf("%s", student.name.fname);
printf("LAST NAME of student %d: ", index);
scanf("%s", student.name.lname);
printf("MIDDLE INITIAL of student %d: ", index);
scanf("%s", &student.name.mi);
printf("ID NUMBER of student %d: ", index);
scanf("%ld", &student.id);
printf("COURSE of student %d: ", index);
scanf("%s", student.course);
printf("YEAR of student %d: ", index);
scanf("%d", &student.year);
printf("GPA of student %d: ", index);
scanf("%f", &student.gpa);
record->student = student;
record->next = (nd *) malloc(sizeof(nd));
record->next->next = NULL;
record = record->next;
getchar();
Pause();
clrscr();
}
void Search(nd *record)
{
int found = FALSE;
long int id;
printf("Student ID to search: ");
scanf("%ld", &id);
getchar();
while (!found && record->next != NULL)
{
if (record->student.id == id)
{
printf("\nThe student you're looking for:\n\n");
DisplayStudRec(record->student);
found = !FALSE;
}
record = record->next;
}
if (!found)
printf("\nSTUDENT ID DOES NOT EXIST!\n");
Pause();
clrscr();
}
void DisplayAllStudentRec(nd *record)
{
int index = 0;
while (record->next != NULL)
{
index++;
clrscr();
printf("----------- MAIN MENU ------------\n\n");
printf("(1) ADD STUDENT RECORD\n");
printf("(2) SEARCH STUDENT RECORD\n");
printf("(3) DISPLAY ALL STUDENT RECORDS\n");
printf("(4) DISPLAY TOP STUDENT\n");
printf("(5) EXIT\n\n");
printf("-- DISPLAY ALL STUDENT RECORDS ---\n\n");
DisplayStudRec(record->student);
record = record->next;
Pause();
if (record->next != NULL)
printf("\n");
clrscr();
}
if (index == 0)
{
printf("NO STUDENT RECORD EXISTS!\n");
Pause();
}
clrscr();
}
void DisplayTopStudent(nd *record)
{
int index = 0;
STUDREC student;
student.gpa = 0;
while (record->next != NULL)
{
index++;
if (student.gpa < record->student.gpa)
student = record->student;
record = record->next;
}
if (index > 0)
{
printf("STUDENT WITH THE HIGHEST GPA IS:\n\n");
DisplayStudRec(student);
}
else
printf("NO STUDENT RECORD EXISTS!\n");
Pause();
clrscr();
}
void DisplayStudRec(STUDREC student)
{
printf("ID Number: %ld\n", student.id);
printf("Name: %s %c %s\n", student.name.fname, student.name.mi, student.name.lname);
printf("Course: %s\n", student.course);
printf("Year: %d\n", student.year);
printf("GPA: %.1f\n", student.gpa);
}
void Pause(void)
{
char input = '\0';
printf("\nPress ENTER to continue...");
while (input != '\n')
input = getchar();
}
@sir MarkCuering: Boxes? Unsa-on mana paghimo bro?