You will develop a program that loads student, course, and

You will develop a program that loads student, course, and grade data from a text file, then allows the user to perform simple queries on that data through a command line interface. The intention of this assignment is to familiarize you with reading data from files, allocating and using dynamically defined 1D and 2D arrays, and further developing your understanding of structs and pointers. This document is separated into four sections: Background, Requirements, Data File, Running the Program, and Submission.

For this assignment, you will develop a program to import and handle course data stored in a text file. Your program must:
● Compile on GCC 5.3 or greater under Xubuntu (or another variant of Ubuntu) 18.04
● Have a struct type (called Student) to hold information about a student. A dynamically allocated array of these will be a global variable.
● Have an enum type (called Academic Level) that defines four values indicating a Student’s grade level – Freshman, Sophomore, Junior and Senior.
● Have a struct type (called Course) which holds course information. A dynamically allocated array of these will be a global variable.
● Have a struct type (called Assign) to hold information about an assignment. A dynamically allocated array of these will be held in your course struct.
● Have a struct type (called ScoreStruct) to hold information about a student’s performance on an assignment. A dynamically allocated array of these will be held in your course struct.
● Your program will support any number of courses, students, and assignments. All dynamic allocation will be dependent on values read from the provided data file.

 

Points for this assignment are divided amongst the following requirements:
● Properly define the Student, Assign, Grade and Course structs and the Academic Level enumeration type
● Successfully load all of the text data into the appropriate structs
● Successfully display all course names in the main menu 

 

 

#ifndef _HW2_H
#define _HW2_H

/**
* This is a basic C program that reads student, course, and grade
* data from a text file
*

*
*/

////////////////////////////////////////////////////////////////////////////////
// INCLUDES
#include
#include
#include
#include

////////////////////////////////////////////////////////////////////////////////
// MACROS: CONSTANTS
#define MAX_STUDENT_NAME_LENGTH 20
#define MAX_COURSE_NAME_LENGTH 10
#define MAX_TEACHER_NAME_LENGTH 20
#define MAX_COMMENT_LENGTH 20
#define MAX_ASSIGN_NAME_LENGTH 20
#define MAX_FILENAME_LENGTH 30

//DATA STRUCTURES

typedef enum AcademicLevel AcademicLevel;
typedef struct ScoreStruct {} ScoreStruct;
typedef struct Assign {} Assign;
typedef struct Student {} Student;
typedef struct Course {} Course;

//GLOBAL VARIABLES
//place to store student information
Student* students = NULL;
//place to store course information
Course* courses = NULL;
int numCourses, numStudents;

//FORWARD DECLARATIONS

// the following should be used to read student/course data in from the file
void readFile(char* filename);
void readString(FILE* file, char** dst, int max_length);
Student* readStudents(FILE* file);
Course* readCourses(FILE* file);
Assign* readAssigns(FILE* file, int numAssigns);
ScoreStruct** readScores(FILE* file, int numAssigns);

// the following should be used to free all heap data allocated during the programs runtime
// and handle dangling pointers
void terminate();
void studentsDestructor();
void coursesDestructor();
void assignsDestructor(Assign** assigns, int numAssign);
void scoresDestructor(ScoreStruct*** scores, int numAssigns);

// the following should be used to cleanly print the data used in the program
void printStudents();
void printAssigns(Assign* assigns, int numAssigns);
void printGrades(ScoreStruct** scores, int numAssigns);
void printCourse(Course course);

// the following are mostly complete functions that define and
// control the CLI menu associated with the program
void mainMenu();
void mainMenuBranch(int option);
void subMenu(Course course);
void subMenuBranch(int option, Course course);

// these are the ‘special’ functions that you are being asked to implement
void studentMenu(Course course);
void getStudentScores(Course course, int studentNo);
void assignmentMenu(Course course);
void getAssignmentScore(Course course, int assignmentNo);

// this is an optional function that parses and executes instructions defined in an input file
void performInstructions(char* iFile);

// this is a utility function to be called when input filenames are invalid
void printUsage();

/////////////////////////////////////////////////////////////////////////////////
//IMPLEMENTATION

/**
* Loads data from student/course data file
* @param filename is the name of the file
*/
void readFile(char* filename){
  FILE* file = fopen(filename, “r”);
  //FOR YOU TO IMPLEMENT
  fclose(file);
}

/**
* Implements main menu functionality, which allows user to select a course to interact with
*/
void mainMenu(){
  int input_buffer;
  printf(“Course Searcher”);  
  do {
    printf(“———————————–“);
    printf(“Course Options”);
    printf(“———————————–“);
    int i;
    for(i = 0; i < numCourses; i++){
      //FOR YOU TO IMPLEMENT
      //printf(”   %d %s”, courses[i].no, courses[i].name);
    }
    printf(”   0 REPEAT OPTIONS”);
    printf(”  -1 TERMINATE PROGRAM”);
    printf(“Please enter your choice —> “);
    scanf(” %d”, &input_buffer);
    mainMenuBranch(input_buffer);
  } while (1);
}

/**
* Handles menu options of main menu
* @param option is the chosen operation’s option #
*/
void mainMenuBranch(int option){
  if (option < -1 || option > numCourses){
    printf(“Invalid input. Please try again…”);;
    while(option < -1 || option > numCourses){
        printf(“Please enter a valid option —> “);
      scanf(” %d”, &option);
    }
  }
  if(option == -1){
    printf(“Terminating program…”);
    terminate();
  } else if (option == 0) {
    printf(“Repeating options…”);
  } else {
    Course course = courses[option – 1];
    //FOR YOU TO IMPLEMENT
    //printf(“Course with name %s selected.”, course.name);
    subMenu(course);
  }
}

/**
* Implements sub menu functionality, which allows users to select how they want to interact with course
* @course is the course to be queried
*/ 

void subMenu(Course course){
  int input_buffer;
  do {
    printf(“———————————–“);
    printf(“Menu Options”);
    printf(“———————————–“);
    printf(”   1 Get a student’s final grades in the course”);
    printf(”   2 Get the average grade of an assignment in the course”);
    printf(”   3 Print all course data”);
    printf(”   0 REPEAT OPTIONS”);
    printf(”  -1 RETURN TO PREVIOUS MENU”);
    printf(”  -2 TERMINATE PROGRAM”);    
    printf(“Please enter your choice —> “);
    scanf(” %d”, &input_buffer);
    subMenuBranch(input_buffer, course);
  } while (input_buffer != -1);
}

/**
* Handles menu options of submenu
* @param option is the chosen operation’s option #
* @param course is the course struct to be queried
*/
void subMenuBranch(int option, Course course){
  if (option < -2 || option > 3){
    printf(“Invalid input. Please try again…”);;
    while(option < -2 || option > 3){
        printf(“Please enter a valid option —> “);
      scanf(” %d”, &option);
    }
  }
  if(option == -2){
    printf(“Terminating program…”);
    terminate();
  } else if (option == -1){
    printf(“Returning to previous menu…”);
  } else if (option == 0){
    printf(“Repeating options…”);
  } else if (option == 1){
    //FOR YOU TO IMPLEMENT    
  } else if (option == 2){
    //FOR YOU TO IMPLEMENT
  } else if (option == 3){
    //FOR YOU TO IMPLEMENT
  }
}

/**
* Prints basic usage instructions for the program to the command line
*/
void print_usage(){
  printf(“USAGE:./LastNameCourseReader -d -c “);
  printf(“-d refers to the required input data file containing student & course information; this must be a valid .txt file”);
  printf(“-i refers to the optionally supported ‘instruction file’ that provides directions for how the program should execute without CLI input; t must be a valid .txt.file”);
}

void terminate(){
  //FREE EVERYTHING HERE
  exit(1);
}

int main(int argc, char* argv[]){
  char* datafile;
  char* instructionfile;
  int opt;
  while((opt = getopt(argc, argv, “:d:i:”)) != -1){
    switch(opt){
      case ‘d’:
        datafile = optarg;
        break;  
      case ‘i’:
        instructionfile = optarg;
        break;
      case ‘:’:
        printf(“option needs a value”);
        break;
      case ‘?’:
        printf(“unknown option: %c”, optopt);
        break;
    }
  }
  for (; optind < argc; optind++){
    printf(“Given extra arguments: %s”, argv[optind]);
  }  

  int dflen;
  if(datafile != NULL){
    dflen = strlen(datafile);
    if(dflen >= 5
        && (strcmp(&datafile[dflen-4], “.txt”) == 0)
        && (access(datafile, F_OK) != -1)){
      printf(“Importing data from %s”, datafile);
      readFile(datafile);
    } else {
      printf(“Data file has an invalid name or does not exist.”);
      print_usage();
      exit(1);  
    }
  } else {
    printf(“No data file name provided. This is a required field.”);
    print_usage();
    exit(1);
  }

  int iflen;
  int ifval;
  if(instructionfile != NULL){
    iflen = strlen(instructionfile);
    if(iflen >= 5
        && (strcmp(&instructionfile[iflen-4], “.txt”) == 0)
        && (access(instructionfile, F_OK) != -1)){
      printf(“Performing instructions defined in %s:”, instructionfile);
      //uncomment below if doing this optional part of the assignment
      //performInstructions(instructionfile);        
    } else {
      printf(“Instruction file has an invalid name or does not exist.”);
      print_usage();
      exit(1);
    }
  } else {
    printf(“No instruction file provided. Using CLI:”);
    mainMenu();
  }
  return 0;
}

#endif

 

Leave a Comment

Your email address will not be published. Required fields are marked *

GradeEssays.com
We are GradeEssays.com, the best college essay writing service. We offer educational and research assistance to assist our customers in managing their academic work. At GradeEssays.com, we promise quality and 100% original essays written from scratch.
Contact Us

Enjoy 24/7 customer support for any queries or concerns you have.

Phone: +1 213 3772458

Email: support@gradeessays.com

© 2024 - GradeEssays.com. All rights reserved.

WE HAVE A GIFT FOR YOU!

15% OFF 🎁

Get 15% OFF on your order with us

Scroll to Top