Question

In: Computer Science

in C++. Implement a structure to handle a college course information. You will also use functions...

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:

  • title; string
  • code; string
  • class_size; integer
  • enrolled; integer
  • roster; array of strings of fixed size 100

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.

Solutions

Expert Solution

//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() {}


Related Solutions

Implement a stored procedure to handle a complete transaction. Also implement a trigger to validate data...
Implement a stored procedure to handle a complete transaction. Also implement a trigger to validate data before a transaction is permitted to execute. Schema: Customer Table CustomerID (PK). FirstName (NOT NULL). LastName (NOT NULL). Address. City. State. Zip. Country. Phone. Email (UNIQUE and NOT NULL). Username (UNIQUE and NOT NULL). Password (NOT NULL). Order Table OrderID (PK). CustomerID (FK). OrderDate. ShipDate. Order Detail Table OrderDetailID (PK). OrderID (FK). ProductID (FK). Price. Quantity (CHECK > 0). Product Table ProductID (PK). CategoryID...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer list using dynamic array ONLY (an array that can grow and shrink as needed, uses a pointer an size of array). Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: class List { public: // Default Constructor List() {// ... } // Push integer n onto...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
C++ please Fill in for the functions for the code below. The functions will implement an...
C++ please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
USE ONLY THE BELOW FUNCTIONS AND IMPLEMENT THE MISSING PART implement the following missing functions from...
USE ONLY THE BELOW FUNCTIONS AND IMPLEMENT THE MISSING PART implement the following missing functions from the implementation: * reset * intersection * difference Set Set::intersection(Set& s){ Set r; // find intersection return r; } Set Set::difference(Set& s){ Set r; // find difference return r; } void Set::reset(int c){ // increase the capacity and clear the data } driver program int a1[] = {10,5,7,3,9}; Set s1(5); s1.insert(a1,5); s1.print("s1"); int a2[] = {2,9,6}; Set s2(3); s2.insert(a2,3); s2.print("s2"); Set s3 = s1.unionset(s2);...
For this program you will implement the following utility functions to test mastery of C strings....
For this program you will implement the following utility functions to test mastery of C strings. *******you MUST use these these function***** void removeBlanks(char *src, char *dest); void replaceChar(char *src, char oldChar, char newChar); char *flipCase(const char *src); Please read the description of these functions carefully. In the removeBlanks function you will implement a routine that takes a string in as src and outputs the same string into dest but removing any blank space character encountered. For example, if the...
Use the following information to answer the next six exercises also using excel functions for the...
Use the following information to answer the next six exercises also using excel functions for the answers: Yoonie is a personnel manager in a large corporation. Each month she must review 16 of the employees. From past experience, she has found that the reviews take her approximately four hours each to do with a population standard deviation of 1.2 hours. Let Χ be the random variable representing the time it takes her to complete one review. Assume Χ is normally...
C coding • Implement, using structures and functions as appropriate, a program which requires you to...
C coding • Implement, using structures and functions as appropriate, a program which requires you to enter a number of points in 3 dimensions. The points will have a name (one alphanumeric character) and three coordinates x, y, and z. Find and implement a suitable way to stop the input loop. The program, through an appropriate distance function, should identify the two points which are the furthest apart. Another function should calculate the centre of gravity of the point cloud...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can assume that any char array passed into the functions will contain valid, null-terminated data. Your functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can...
A C++ question: Implement the following functions. Each function deals with null terminated C-strings. You can assume that any char array passed into the functions will contain valid, null-terminated data. Your functions must have the signatures listed below. 1. This function returns the last index where the target char can be found in the string. it returns -1 if the target char does not appear in the string. For example, if s is “Giants” and target is ‘a’ the function...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT