- #include<stdio.h>
- #include<string.h>
- #include<conio.h>
- #include<stdlib.h>
- #define MENU 100
- struct
- {
- int Total;
- int Deleted;
- char filler [16];
- }header;
- struct
- {
- char Student_number[6];
- char Last_name [16];
- char First_name [16];
- char Address [26];
- char Program_code [6];
- char Program_year [1];
- }student;
- void addstudent(void);
- void list (void);
- void deletestudent (void);
- main()
- {
- //declaring variables
- char menu,i;
- //entering a for loop so that the menu reappears after each function
- for (i=0; i <MENU; i++)
- {
- //clears the screen
- system("cls");
- //prints instructions to screen
- printf("Main Menu\n\n1)Add Student\n2)Delete Student\n3)List Students");
- printf("\n4)Compact Records\n5)Exit program\n\n");
- //scans in what function the user wants to use
- scanf("%1s",&menu);
- //switch statement to impliment different functions depending on the users choice
- switch (menu)
- {
- //if 1 is chosen the screen will clear and the numbers will be read in, error checked and counted
- case '1':
- addstudent();
- break;
- case '2':
- deletestudent ();
- break;
- case '3':
- list();
- break;
- case '5':
- exit(EXIT_SUCCESS);
- //if a character not in the switch is chosen a error will print to screen for barely a second and the loop will reset so the user can pick an approiate answer
- default:
- printf("ERROR");
- }//End switch
- }//End for
- //keeps program open
- getch();
- }//End main
- void addstudent()
- {
- FILE *library;
- char choice = 'y';
- char Student_number_found = 'n';
- char test_Student_number[6];
- library = fopen("student.dat","ab+");
- while (choice == 'y'||choice == 'Y')
- {
- do{
- printf("\nEnter Student Number:");
- scanf("%s",test_Student_number);
- Student_number_found = 'n';
- fseek(library,sizeof(header),SEEK_SET);
- while(fread(&student,sizeof(student),1,library)==1)
- {
- if(strcmp(student.Student_number,test_Student_number)==0)
- {
- Student_number_found = 'Y';
- fseek(library,0,SEEK_END);
- printf("\n Student Number already entered please try again....");
- }
- }
- }
- while(Student_number_found == 'Y');
- {
- strcpy(student.Student_number,test_Student_number);
- printf("\nEnter Last name:");
- scanf("%s",student.Last_name);
- printf("\nEnter First name:");
- scanf("%s",student.First_name);
- printf("\nEnter Address:");
- scanf("%s",student.Address);
- printf("\nEnter Program Code:");
- scanf("%s",student.Program_code);
- printf("\nEnter Program Year:");
- scanf("%s",student.Program_year);
- fwrite(&student,sizeof(student),1,library);
- printf("\nAdd Another record (y/n):");
- scanf("%1s",&choice);
- }
- fclose(library);
- }
- }
- void list()
- {
- FILE *library;
- library = fopen("student.dat","rb");
- printf("\n Student No.\tLast Name\tFirst Name\tAddress\tP Code\tP year\n");
- printf(" ===========================================================================\r");
- while(fread(&student,sizeof(student),1,library)==1)
- {
- printf("\n %s \t\t%s \t\t%s \t\t%s\t%s\t%s\n",
- student.Student_number,student.Last_name,student.First_name,
- student.Address,student.Program_code,student.Program_year
- );
- }
- fclose(library);
- getch();
- }
- void deletestudent ()
- {
- }