Project for CSE115 Lab (C Programming)




/*  Project Members:

*/
//including header files
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
//Functions declaring space
void pass();
void menu();
void addRecord();
void editRecord();
void deleteRecord();
void checkDetails();
void listofAllStudents();
void exit_f();
//Defining structures
typedef struct{
  int day,month,year;
} date;
typedef struct
{
  char ID[5];
  char first_name[50];
  char last_name[50];
  char gender[6];
  date dob;
  char phone_number[14];
  char department[10];
  char roomNumber[4];
} studentInfo;
//Main function
int main()
{
  pass();
  menu();

return 0;
}
//password function
void pass()
{
char name[20];
char password[10];
 repeat:
printf("\n\n\n\t\t\xB2\xB2\xB2\xB2\xB2\xB2\xB2 For accessing data please login \xB2\xB2\xB2\xB2\xB2\xB2\xB2\n\n");
printf(" \t\t\tEnter username: ");
scanf("%s",name);
printf("\t\t\tEnter password: ");
scanf("%s",password);
system("CLS");
if (strcmp(name, "Admin") == 0 && strcmp(password, "pass") == 0)
printf("\t\t\tAccess granted.\n");
else
{
    printf("\t\t\tWrong username/password. Try again!\n");
    printf("\t\t\t===================================\n\n");
    goto  repeat;
}

}
//menu function
void menu()
{
    system("cls");

  printf("\n\n\n\t\t\xB2\xB2\xB2\xB2\xB2\xB2\xB2 Welcome to Hostel Management System \xB2\xB2\xB2\xB2\xB2\xB2\xB2\n\n");
  printf("\t\tProject by\n\t\t\xB2\xB2\xB2 Rubyda Hossain\n\t\t\xB2\xB2\xB2 Atanu Das\n\t\t\xB2\xB2\xB2 \n");
  printf("\t\t===================================================\n");
  printf("   \t\t\t\t   Main Menu\n");
  printf("\t\t===================================================\n\n");
  printf("\t\t\xB2\xB2\xB2  1. Add new record\n\t\t\xB2\xB2\xB2\n");
  printf("\t\t\xB2\xB2\xB2  2. Edit/Update record\n\t\t\xB2\xB2\xB2\n");
  printf("\t\t\xB2\xB2\xB2  3. Erase record\n\t\t\xB2\xB2\xB2\n");
  printf("\t\t\xB2\xB2\xB2  4. Check the details of a students record\n\t\t\xB2\xB2\xB2\n");
  printf("\t\t\xB2\xB2\xB2  5. View the list of all students\n\t\t\xB2\xB2\xB2\n");
  printf("\t\t\xB2\xB2\xB2  6. Exit\n\n\n");

  printf("\t\t     Press the corresponding number to select the option: ");

  switch(getch())
  {
    case '1':
        addRecord();
        break;
    case '2':
        editRecord();
        break;
    case '3':
        deleteRecord();
        break;
    case '4':
        checkDetails();
        break;
    case '5':
        listofAllStudents();
        break;
    case '6':
        exit_f();
        break;
    default:
        menu();
  }
}

void addRecord()
{
  system("CLS");
  studentInfo add,check;

  FILE *pt1;
  int menuOrExit;
  pt1=fopen("hostel_directory.txt","a+");
  printf("\t Add New students Data\n\n");
  printf("\t Enter a student's ID (4 digit) whose information you want to add to the directory: \n\t ");
  scanf("%s",check.ID);
  fflush(stdin);
  while(fscanf(pt1,"%s %s %s %s %d %d %d %s %s %s\n", add.ID, add.first_name, add.last_name, add.gender, &add.dob.day,&add.dob.month, &add.dob.year, add.phone_number, add.department, add.roomNumber)!=EOF)
  {
    if(strcmp(check.ID,add.ID)==0)
    {
      printf("\n\t Student ID already in use!");
      printf("\n\n\t Now enter (1) if you wish to go back to the main menu.\n\t Or  enter (0) to exit the program:  ");
      scanf("%d",&menuOrExit);
      if(menuOrExit==1)
        {
            menu();
        }
      else if(menuOrExit==0)
        {
        exit_f();
        }
    }

  }
  strcpy(add.ID,check.ID);
  printf("\nEnter the first name of the student: ");
  scanf("%s",add.first_name);
  fflush(stdin);
  printf("Enter the last name of the student: ");
  scanf("%s",add.last_name);
  fflush(stdin);
  printf("Enter their gender(Male,Female or Other):");
  scanf("%s",add.gender);
  fflush(stdin);
  printf("Enter his/her date of birth (with spaces)(DD MM YYYY): ");
  scanf("%d %d %d",&add.dob.day,&add.dob.month, &add.dob.year);
  fflush(stdin);
  printf("Enter their phone number: ");
  scanf("%s",add.phone_number);
  fflush(stdin);
  printf("Enter their department:");
  scanf("%s",add.department);
  fflush(stdin);
  printf("Enter their hostel room number (3 digit):");
  scanf("%s",add.roomNumber);
  fflush(stdin);
  fprintf(pt1,"%s %s %s %s %d %d %d %s %s %s\n", add.ID, add.first_name, add.last_name, add.gender, add.dob.day,add.dob.month, add.dob.year, add.phone_number, add.department, add.roomNumber);

  fclose(pt1);
  printf("Student information added successfully!");
  printf("\n\n\t Now enter (1) if you wish to go back to the main menu.\n\t Or  enter (0) to exit the program:  ");
  scanf("%d",&menuOrExit);
  if(menuOrExit==1)
  {
     menu();
  }
  else if(menuOrExit==0)
  {
     exit_f();
  }

}

void editRecord()
{
  system("CLS");
  studentInfo update,check;

  FILE *pt1,*pt2;
  int menuOrExit,choice,flag=0;
  pt1=fopen("hostel_directory.txt","r");
  pt2=fopen("New_File.txt","w");
  if(pt2==NULL)
  {
      printf("Could not open a new file to write.");
      printf("Redirecting you to main menu.");
      menu();

  }
  printf("\t Edit/Update students Data\n\n");
  printf("\t Enter a student's ID (4 digit) whose information you want to update in the directory: \n\t ");
  scanf("%s",check.ID);
  while(fscanf(pt1,"%s %s %s %s %d %d %d %s %s %s\n", update.ID, update.first_name, update.last_name, update.gender, &update.dob.day,&update.dob.month, &update.dob.year, update.phone_number, update.department, update.roomNumber)!=EOF)
  {
    if(strcmp(update.ID,check.ID)!=0)
    {
        fprintf(pt2,"%s %s %s %s %d %d %d %s %s %s\n", update.ID, update.first_name, update.last_name, update.gender, update.dob.day,update.dob.month, update.dob.year, update.phone_number, update.department, update.roomNumber);
    }
    else if(strcmp(update.ID,check.ID)==0)
       {
           flag=1;
            printf("\n\t If you want to change the phone number press (1).\n\t If you want to change the hostel room number press (2).\n\t ");
            scanf("%d",&choice);
            if(choice==1)
            {
                fprintf(pt2,"%s %s %s %s %d %d %d ",update.ID, update.first_name, update.last_name, update.gender, update.dob.day,update.dob.month, update.dob.year);
                printf("Enter the new phone number: ");
                scanf("%s",update.phone_number);
                fprintf(pt2,"%s ",update.phone_number);
                fprintf(pt2,"%s %s\n", update.department, update.roomNumber);

            }
            else if(choice==2)
            {
                fprintf(pt2,"%s %s %s %s %d %d %d %s %s ",update.ID, update.first_name, update.last_name, update.gender, update.dob.day,update.dob.month, update.dob.year,update.phone_number,update.department);
                printf("Enter the new hostel room number (3 digit): ");
                scanf("%s",update.roomNumber);
                fprintf(pt2,"%s\n",update.roomNumber);
            }
       }

  }
  fclose(pt1);
  fclose(pt2);
  remove("hostel_directory.txt");
  rename("New_File.txt","hostel_directory.txt");
  if(flag==0)
  {
      printf("Student ID does not exist in the directory.");
  }
  else if(flag==1)
  {
    printf("Student info. updated successfully!");
  }
  printf("\n\n\t Now enter (1) if you wish to go back to the main menu.\n\t Or  enter (0) to exit the program:  ");
  scanf("%d",&menuOrExit);
  if(menuOrExit==1)
  {
     menu();
  }
  else if(menuOrExit==0)
  {
     exit_f();
  }

}

void deleteRecord()
{
    system("CLS");
  studentInfo data,check;

  FILE *pt1,*pt2;
  int menuOrExit,flag=0;
  pt1=fopen("hostel_directory.txt","r");
  pt2=fopen("New_File.txt","w");
  if(pt2==NULL)
  {
      printf("Could not open a new file to write.");
      printf("Redirecting you to main menu.");
      menu();

  }
  printf("\tErase a students Data\n\n");
  printf("\tEnter a student's ID (4 digit) whose information you want to erase from the directory: \n\t");
  scanf("%s",check.ID);
  while(fscanf(pt1,"%s %s %s %s %d %d %d %s %s %s\n", data.ID, data.first_name, data.last_name, data.gender, &data.dob.day,&data.dob.month, &data.dob.year, data.phone_number, data.department, data.roomNumber)!=EOF)
  {
    if(strcmp(data.ID,check.ID)==0)
    {
        flag=1;
        continue;
    }
    else if(strcmp(data.ID,check.ID)!=0)
    {
        fprintf(pt2,"%s %s %s %s %d %d %d %s %s %s\n", data.ID, data.first_name, data.last_name, data.gender, data.dob.day,data.dob.month, data.dob.year, data.phone_number, data.department, data.roomNumber);
    }

  }
  fclose(pt1);
  fclose(pt2);
  remove("hostel_directory.txt");
  rename("New_File.txt","hostel_directory.txt");
  if(flag==0)
  {
        printf("Student ID does not exist in the directory!");
  }
  else if(flag==1)
  {
      printf("Student info. erased successfully.");
  }
  printf("\n\n\t Now enter (1) if you wish to go back to the main menu.\n\t Or  enter (0) to exit the program:  ");
  scanf("%d",&menuOrExit);
  if(menuOrExit==1)
  {
     menu();
  }
  else if(menuOrExit==0)
  {
     exit_f();
  }

}

void checkDetails()
{
  system("CLS");
  studentInfo check,temp;

  FILE *pt1;
  int menuOrExit,flag=0;
  pt1=fopen("hostel_directory.txt","r");
  printf("\tCheck details of a student\n\n");
  printf("\tEnter the ID (4 digit) of a student whose details you want to see: \n\t");
  scanf("%s",temp.ID);
  while(fscanf(pt1,"%s %s %s %s %d %d %d %s %s %s\n", check.ID, check.first_name, check.last_name, check.gender, &check.dob.day,&check.dob.month, &check.dob.year, check.phone_number, check.department, check.roomNumber)!=EOF)
  {
    if(strcmp(check.ID,temp.ID)==0)
    {
        printf("\nThe details of the student is:\n");
        printf("\n\nID   Name    Gender Date of Birth  Phone number  Department  Hostel room number\n");
        printf("%s %s %s %s %d %d %d %s %s %s\n", check.ID, check.first_name, check.last_name, check.gender, check.dob.day,check.dob.month, check.dob.year, check.phone_number, check.department, check.roomNumber);
        flag=1;
        break;
    }

  }
  if(flag==0)
  {
      printf("The student ID does not exist in directory!");
  }
  fclose(pt1);
  printf("\n\n\t Now enter (1) if you wish to go back to the main menu.\n\t Or  enter (0) to exit the program:  ");
  scanf("%d",&menuOrExit);
  if(menuOrExit==1)
  {
     menu();
  }
  else if(menuOrExit==0)
  {
     exit_f();
  }

}

void listofAllStudents()
{
  system("CLS");
  studentInfo listmember;

  FILE *pt1;
  int menuOrExit;
  pt1=fopen("hostel_directory.txt","r");
  printf("\tViewing the list of students\n\n");
  printf("\nThe list of all the students with all of their information:\n");
  printf("\n\nID   Name    Gender Date of Birth  Phone number  Department  Hostel room number\n\n");
  while(fscanf(pt1,"%s %s %s %s %d %d %d %s %s %s\n", listmember.ID, listmember.first_name, listmember.last_name, listmember.gender, &listmember.dob.day,&listmember.dob.month, &listmember.dob.year, listmember.phone_number, listmember.department, listmember.roomNumber)!=EOF)
  {
        printf("%s %s %s %s %d %d %d %s %s %s\n\n", listmember.ID, listmember.first_name, listmember.last_name, listmember.gender, listmember.dob.day,listmember.dob.month, listmember.dob.year, listmember.phone_number, listmember.department, listmember.roomNumber);

  }
  fclose(pt1);
  printf("\n\n\t Now enter (1) if you wish to go back to the main menu.\n\t Or  enter (0) to exit the program:  ");
  scanf("%d",&menuOrExit);
  if(menuOrExit==1)
  {
     menu();
  }
  else if(menuOrExit==0)
  {

     exit_f();

  }

}
//exit function
void exit_f()
{

  printf("\n\n\t\t\xB2\xB2\xB2\xB2\xB2\xB2\xB2 Do you really want to quit? \xB2\xB2\xB2\xB2\xB2\xB2\xB2\n\n");
  printf("   \t\t\t 1. Yes\n\n");
  printf("   \t\t\t 2. No\n");
  switch(getch())
  {

    case '1':
      exit(0);
      break;
    case '2':
      menu();
      break;
    default:
      system("CLS");

      printf("\t\t\tInvalid input!\n");
      printf("\t\t\t==============");
      exit_f();

  }

}

Comments