In: Computer Science
in C++.
Implement a structure to handle a college course information. You will also use functions to manipulate the structure. Follow the instructions step by step to complete the homework successfully.
(1) Create a structure course. The structure should include the following attributes:
Important! The names of the structure and each of its field must match exactly for the program to work and be graded correctly.
Before moving on with the exercise, it is recommended that you self-test your structure in the main function (create a structure, assign values and print them…). Just make sure to remove these changes from the main before you continue.
(2) function getCourseFromFile(). This function takes as argument the name of a file where course information is stored, reads the file, and returns a structure with that information.
The format of the file is the following:
<Course name> <Course code> <Course class size> <Student1> <Student2> <Student3> ...
In the function, open a file stream using the file name passed as argument. If the file is not opened successfully, print
Error! File not found.
Else, read the information from the file into a local structure variable (the variable to be returned). You can assume that the number of students in the file will not exceed the size of the array.
Note that the number of students enrolled is not know and has to be determined while reading the list of names in the file.
This function is tested using unit testing.
(3) function checkCourseSize(). This function takes as argument a course structure and returns true if the number of students enrolled is less or equal the class size, and false otherwise.
This function is tested using unit testing.
(4) function printRoster(). This function takes as argument a course structure and prints all the students to standard output, each one on a new line.
(5) function saveCourseSummary(). This function takes two arguments: a string with the desired output file name, and a course structure. Then saves in the output file the following course summary:
Course title: <title> Course code: <code> Class size: <size> Students enrolled: <enrolled>
Additionally, it uses the function checkCourseSize and if the function returns false (too many students enrolled), it adds to the file the following message:
Enrollment exceeds class size. Drop students or find bigger classroom.
//The required code is as follows
//The code is self explanatory according to the question
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
//creating a structure for course
struct course {
string title;
string code;
int class_size;
int enrolled;
string roster[100];
};
//function getCourseFromFile()
struct course getCourseFromFile(string filename) {
ifstream fin(filename);
//check if fin doesn't open successfully
if(!fin) {
cout << "Error! File not found.";
exit(1);
}
struct course c;
fin >> c.title >> c.code >> c.class_size;
c.enrolled = 0;
while(!fin.eof()) {
fin >> c.roster[c.enrolled];
c.enrolled++;
}
fin.close();
return c;
}
//function checkCourseSize()
bool checkCourseSize(struct course c) {
if(c.enrolled <= c.class_size)
return true;
else
return false;
}
//function printRoster()
void printRoster(struct course c) {
for(int i = 0; i < c.enrolled; i++)
cout << c.roster[i] << "\n";
}
//function saveCourseSummary()
void saveCourseSummary(string filename, struct course c) {
ofstream fout;
fout.open(filename, ios::trunc);
if(!fout) {
exit(2);
}
fout << "Course title: " << c.title << "\n";
fout << "Course code: " << c.code << "\n";
fout << "Class size: " << c.class_size << "\n";
fout << "Students enrolled: " << c.enrolled << "\n";
if(checkCourseSize(c) == false) {
fout << "Enrollment exceeds class size. Drop students or find bigger classroom." ;
}
fout.close();
}
int main() {}