C++ Question
Objectives
After this homework assignment, students should be able to:
Implement user-defined structs using given specifications
Use file input to populate structs
Background
While working to improve the LionPath website, you discovered a way to generate formatted text files that
contains information about students' schedules for the upcoming semester. Consider the example below:
FNAME: Andrew
MINIT: S
LNAME: Yu
ID: 912345678
USER: auy77
CMPSC121-002-003L
- M 9 05 AM 50 Lecture
- T 10 05 AM 110 Lab
- W 9 05 AM 50 Lecture
MATH140-001
- M 12 25 PM 50 Lecture
- T 12 25 PM 50 Lecture
- W 12 25 PM 50 Lecture
- R 12 25 PM 50 Lecture
PSU007-001
- W 06 00 PM 50 Lecture
ENGL101-004
- M 11 15 AM 50 Lecture
- W 11 15 AM 50 Lecture
- F 11 15 AM 50 Lecture
PHYS211-003-006L
- T 3 35 PM 90 Lecture
- R 3 35 PM 90 Lecture
- F 12 00 PM 110 Lab
Each line that begins with "-" refers to one of the meeting times of the course above. For example:
- M 9 05 AM 50 Lecture Monday, 9:05AM, 50 minute lecture
Because LionPath can generate many of these files, you have decided to write a C++ program that can automatically process them.
You will use object-oriented programming to keep track of the students and courses.
Instructions
Write a C++ program named lionpath.cpp that implements the structs described below:
Student
The Student struct contains basic identifying information about one student and a vector of courses that the student is scheduled to take:
Attribute Data Type
First name string
Middle initial char
Last name string
9-digit PSU ID Number string
PSU Username string
Enrolled Courses vector<Course>
Course
The Course struct contains the following information:
Attribute Data Type
Course name string
Number of lectures per week int
-------------------------------------------------------------------
Write a main function that:
Reads a text file (like shown in the background)
Automatically populates a Student struct with relevant information
Displays a simplified version of it to the console window
Note that only the number of lectures per course needs to be counted, which means the content of the lines
containing information about each lecture can be ignored.
Optional Bonus (+2 points)
Write a C++ program named schoolday.cpp that extends the functionality lionpath.cpp by:
Accepting user input for a character representing a weekday:
o M, T, W, R, or F
Printing information about all lectures scheduled on that day
-----------------------------------------------------------------
Sample Output
FNAME: Andrew
MINIT: S
LNAME: Yu
ID: 912345678
USER: auy77
CMPSC121-002-003L
3 lectures
MATH140-001
4 lectures
PSU007-001
1 lecture
ENGL101-004
3 lectures
PHYS211-003-006L
3 lectures
-----------------------------------------------------------
Optional Bonus Sample Output
FNAME: Andrew
MINIT: S
LNAME: Yu
ID: 912345678
USER: auy77
Select a weekday (M, T, W, R, F): S
Invalid input, try again!
Select a weekday (M, T, W, R, F): T
CMPSC121-002-003L
-T 10 05 AM 110 Lab
MATH140-001-
T 12 25 PM 50 Lecture
PSU007-001
ENGL101-004
PHYS211-003-006L
-T 3 35 PM 90 Lecture
PROGRAM:
#include <iostream>
#include <stdlib.h>
#include <string>
#include <fstream>
#include <vector>
#define MAX 100
using namespace std;
// Defines a structure Course
struct Course
{
// Data member to store course data
string courseName;
string courseInfo[MAX];
int lecturesPerWeek;
};// End of structure
// Defines a structure Student
struct Student
{
// Data member to store student data
string firstName;
char middleInitial;
string lastName;
string psuID;
string psuUserName;
vector <Course> enrolledCourses;
};// End of structure
// Function to read file contents and store it in student
and course structure objects
void ReadFile(Student &students, Course courses[], int
&numCour, int &numSub)
{
// Declares an object of ifstream
ifstream inFile;
// To store heading tags
string heading;
// To store new line character
char newLineChar;
// Counter for course time
int counter = 0;
// Open file for reading
inFile.open("Course.txt");
// Checks whether file can be opened or not
if(inFile.fail())
{
cout << "No Such File" << endl;
exit(0);
}// End of if condition
// Reads heading and student information
inFile>>heading;
inFile>>students.firstName;
inFile>>heading;
inFile>>students.middleInitial;
inFile>>heading;
inFile>>students.lastName;
inFile>>heading;
inFile>>students.psuID;
inFile>>heading;
inFile>>students.psuUserName;
inFile>>newLineChar;
// Loops till not end of file
while (!inFile.eof())
{
// Reads the course data
getline(inFile, heading);
// Checks if the first character of the data is not
'-'
// then read data is course name
if(heading.at(0) != '-')
{
// Stores the read data at numCour index position for course
name
courses[numCour].courseName = heading;
// Increase the subject counter by one
numSub++;
// Checks if number of courses is 0 then do nothing
if(numCour == 0)
;
// Otherwise assigns the counter value at numCour index
position as course time
else
courses[numCour].lecturesPerWeek = counter;
// Reset the course time counter to 0
counter = 0;
// Assigns the course current data to vector
students.enrolledCourses.push_back(courses[numCour]);
// Increase the counter by one
numCour++;
}// End of if condition
// Otherwise assign the read data at counter index position
for course time
else
courses[numCour].courseInfo[counter++] = heading;
// Checks if it is end of file
if(inFile.eof())
{
// Assigns the counter value at numCour index position as course
time
courses[numCour].lecturesPerWeek = counter;
// Assigns the course current data to vector
students.enrolledCourses.push_back(courses[numCour]);
}// End of if condition
}// End of while loop
// Close the file
inFile.close();
}// End of function
// Function to display information
void showCourses(Student students, Course courses[], int numCou,
int numSub)
{
// Displays student information
cout<<"\n FNAME: "<<students.firstName<< "\n
MINIT: "<<students.middleInitial
<<"\n LNAME: "<<students.lastName<<"\n ID:
"<<students.psuID
<<"\n USER:
"<<students.psuUserName<<"\n\n";
// Loops till number of subjects
for(int c = 0; c < numSub; c++)
// Displays subject name and number of lecturers
cout<<"\n "<<courses[c].courseName<<endl<<"
"<<courses[c+1].lecturesPerWeek<<"
lectures."<<endl;
}// End of function
// main function definition
int main()
{
// Creates a student object
Student students;
// Creates an array of course object
Course courses[MAX];
// Number of courses and number of subject counter
int numCou = 0, numSub = 0;
// Calls the function to read file
ReadFile(students, courses, numCou, numSub);
// Calls the function to show information
showCourses(students, courses, numCou, numSub);
}// End of function
C++ Question Objectives After this homework assignment, students should be able to: Implement user-defined structs...