Answer to Question #205282 in C for Hassam

Question #205282

Write a program that is able to store the record of 30 students using structure and filing in C language. The Data contain roll number (Numeric part only i.e. 007), Registration number (Sp03-BCE-007), Name, Father name, semester result (at least 4 courses marks) and CGPA. Program user will be able to add new input data (if data limit not exceeded already), edit selected data and delete selected data. Also write a function that enable program user to search database with help of either roll number or name. Once match occurs program will display all the relevant credentials (data). Your program main screen displays a menu that give all those options i.e. new, edit, delete, search etc.

You need to put all the checks to restrict user from illegal inputs. Once you input the data and successfully create data file, your program upon executing will able to open the same file without losing any data until user give command through proper instruction (Program menu).

also provides its flow chart and comment on it.


1
Expert's answer
2021-06-10T15:24:23-0400
#include <stdio.h>
#include <stdlib.h>

#define STR_SIZE 20
#define STUD_SIZE 2

struct Date
{
  int year;
  int month;
  int day;
};

struct Address
{
  char index[6];
  char country[STR_SIZE];
  char city[STR_SIZE];
  char street[STR_SIZE];
  int house;
  int flat;
};

struct Student
{
  char surname[STR_SIZE];
  char name[STR_SIZE];
  struct Date dateOfBirth;
  char phone[11];
  struct Address homeAddress;
  char university[STR_SIZE];
  int course;
  char group[6];
  float average;
  char specialization[STR_SIZE];

};

void clear_buffer()
{
  char ch;
  while ((ch = getchar()) != '\n' && ch != EOF);
}

void FillArray(struct Student* students)
{
  int i;
  for (i = 0; i < STUD_SIZE; i++)
  {
    printf("Enter the surname of student #%d: ", i + 1);
    scanf_s("%s", students[i].surname, 20);
    clear_buffer();
    printf("Enter the name of student #%d: ", i + 1);
    scanf_s(" %s", students[i].name, 20);
    clear_buffer();
    printf("Now let's enter the date of birth of student #%d: \n", i + 1);
    printf("Year: ");
    scanf_s("%d", &students[i].dateOfBirth.year);
    clear_buffer();
    printf("Month: ");
    scanf_s("%d", &students[i].dateOfBirth.month);
    clear_buffer();
    printf("Day: ");
    scanf_s("%d", &students[i].dateOfBirth.day);
    clear_buffer();
    printf("Enter the phone number of student #%d(format: 0502393322): ", i + 1);
    scanf_s(" %s", students[i].phone, 11);
    clear_buffer();
    printf("Now we are going to enter the address of student #%d: \n", i + 1);
    printf("Index: ");
    scanf_s(" %s", students[i].homeAddress.index, 6);
    clear_buffer();
    printf("Country: ");
    scanf_s(" %s", students[i].homeAddress.country, 20);
    clear_buffer();
    printf("City: ");
    scanf_s(" %s", students[i].homeAddress.city, 20);
    clear_buffer();
    printf("Street: ");
    scanf_s(" %s", students[i].homeAddress.street, 20);
    clear_buffer();
    printf("House: ");
    scanf_s(" %d", &students[i].homeAddress.house);
    clear_buffer();
    printf("Flat: ");
    scanf_s("%d", &students[i].homeAddress.flat);
    clear_buffer();
    printf("Name of university of student #%d: ", i + 1);
    scanf_s(" %s", students[i].university, 20);
    clear_buffer();
    printf("Course of student #%d: ", i + 1);
    scanf_s(" %d", &students[i].course);
    clear_buffer();
    printf("Group of student #%d: ", i + 1);
    scanf_s("%s", students[i].group, 6);
    clear_buffer();
    printf("Average score of student #%d: ", i + 1);
    scanf_s(" %f", &students[i].average);
    clear_buffer();
    printf("Specialization of student #%d: ", i + 1);
    scanf_s(" %s", students[i].specialization, 20);
    clear_buffer();
  }
}

void FindStudents(struct Student* students)
{
  char phone[11];
  printf("Enter phone number to find student: ");
  scanf_s("%s", phone, 11);
  int i;
  for (i = 0; i < STUD_SIZE; i++)
  {
    if (!strcmp(students[i].phone, phone))
    {
      printf("Student: %s %s\n", students[i].name, students[i].surname);
      printf("Birthday: %d.%d.%d\n", students[i].dateOfBirth.day,
        students[i].dateOfBirth.month, students[i].dateOfBirth.year);
      printf("Telephone number: %s\n", students[i].phone);
      printf("Address: %s, %s, %s city, %s st., %d, flat %d\n",
        students[i].homeAddress.index, students[i].homeAddress.country,
        students[i].homeAddress.city, students[i].homeAddress.street,
        students[i].homeAddress.house, students[i].homeAddress.flat);
      printf("University: %s\n", students[i].university);
      printf("Course: %d\n", students[i].course);
      printf("Group: %s\n", students[i].group);
      printf("Average score: %f\n", students[i].average);
      printf("Specialization: %s\n\n", students[i].specialization);
    }
  }
}

void PrintStudents(struct Student* students)
{
  int i;
  for (i = 0; i < STUD_SIZE; i++)
  {
    printf("Student: %s %s\n", students[i].name, students[i].surname);
    printf("Birthday: %d.%d.%d\n", students[i].dateOfBirth.day, students[i].dateOfBirth.month, students[i].dateOfBirth.year);
    printf("Telephone number: %s\n", students[i].phone);
    printf("Address: %s, %s, %s city, %s st., %d, flat %d\n", students[i].homeAddress.index, students[i].homeAddress.country, students[i].homeAddress.city, students[i].homeAddress.street, students[i].homeAddress.house, students[i].homeAddress.flat);
    printf("University: %s\n", students[i].university);
    printf("Course: %d\n", students[i].course);
    printf("Group: %s\n", students[i].group);
    printf("Average score: %f\n", students[i].average);
    printf("Specialization: %s\n\n", students[i].specialization);
  }
}

void SortStudents(struct Student* students)
{
  struct Student tmp;
  for (int i = 0; i < STUD_SIZE; i++)
  {
    for (int j = i + 1; j < STUD_SIZE; j++)
    {
      if ((students[i].dateOfBirth.year > students[j].dateOfBirth.year)
        || (students[i].dateOfBirth.year == students[j].dateOfBirth.year
          && students[i].dateOfBirth.month > students[j].dateOfBirth.month)
        || (students[i].dateOfBirth.year == students[j].dateOfBirth.year
          && students[i].dateOfBirth.month == students[j].dateOfBirth.month
          && students[i].dateOfBirth.day > students[j].dateOfBirth.day))
      {
        tmp = students[i];
        students[i] = students[j];
        students[j] = tmp;
      }
    }
  }
}


void SafeToFile(struct Student* students)
{
  FILE* file;
  errno_t err;
  err = fopen_s(&file, "students.txt", "w");
  if (file == NULL)
  {
    printf("File wasn't opened\n");
    exit(1);
  }
  int i;
  for (i = 0; i < STUD_SIZE; ++i)
  {
    fprintf(file, "%s\n", students[i].surname);
    fprintf(file, "%s\n", students[i].name);
    fprintf(file, "%d\n", students[i].dateOfBirth.year);
    fprintf(file, "%d\n", students[i].dateOfBirth.month);
    fprintf(file, "%d\n", students[i].dateOfBirth.day);
    fprintf(file, "%s\n", students[i].phone);
    fprintf(file, "%s\n", students[i].homeAddress.index);
    fprintf(file, "%s\n", students[i].homeAddress.country);
    fprintf(file, "%s\n", students[i].homeAddress.city);
    fprintf(file, "%s\n", students[i].homeAddress.street);
    fprintf(file, "%d\n", students[i].homeAddress.house);
    fprintf(file, "%d\n", students[i].homeAddress.flat);
    fprintf(file, "%s\n", students[i].university);
    fprintf(file, "%d\n", students[i].course);
    fprintf(file, "%s\n", students[i].group);
    fprintf(file, "%f\n", students[i].average);
    fprintf(file, "%s\n", students[i].specialization);
  }
  fclose(file);
}

void ReadFromFile(struct Student* students)
{
  FILE * file;
  errno_t err;
  err = fopen_s(&file, "students.txt", "r");
  if (file == NULL)
  {
    printf("File wasn't opened\n");
    exit(1);
  }
  rewind(file);
  char buf[STR_SIZE];
  while (fgets(buf, STR_SIZE, file))
  {

  }
  fclose(file);
}

int main()
{
  struct Student* students;
  students = (struct Student*)malloc(STUD_SIZE * sizeof(struct Student));
  int menu = 1;
  while (menu)
  {
    int choice;
    printf("\nWhat you wanna do?\n1. Fill the array\n2. Find student\n3. Print students' info\n4. Save to file\n5. Read from file\n6. Exit\n");
    scanf_s("%d", &choice);
    switch (choice)
    {
    case 1:
      FillArray(students);
      break;
    case 2:
      FindStudents(students);
      break;
    case 3:
      PrintStudents(students);
      break;
    case 4:
      SafeToFile(students);
      break;
    case 5:
      clear_buffer();
      ReadFromFile(students);
      break;
    default:
      menu = 0;
      break;
    }
  }
  free(students);
  return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS