2.Your task for this portion of the assignment is to choose one of these member
ID: 3669934 • Letter: 2
Question
2.Your task for this portion of the assignment is to choose one of these member functions which you used in part-1 of this assignment to perform the comparison and make it a non-member of the struct. You will need to make the appropriate changes accordingly. Your program should also compile and run. The user of the program should not notice any difference when running the program with any of your implementations for Part-1 and Part-2 of this assignment.
Heres what i have:
#include <iostream> //for cout, cin
#include <iomanip> // for setw, fixed, setprecision() manipulators
#include <fstream> //for file input/output
#include <string.h> //for string types usage
#include <cstdlib>
using namespace std;
/*
Structure to store the date
*/
struct Date
{
int month;
int day;
int year;
};
/*
Structure to store students data
*/
struct Student
{
string firstname;
string lastname;
string uin;
string major;
float gpa;
char graduationStatus;
Date graduationDate;
};
Student* readStudentsFromFile(int &totalStudents);
void menu(Student *students, int totalStudents);
void printHorizontalLine( int width, char border_char );
void printStudents(Student *students, int totalStudents, char graduationStatusFlag = 'N', float gpaThreshold = -1, string majorFilter = "");
/*
* Entry point
*/
int main()
{
Student *allStudentsPointer;
int totalStudents = 0;
allStudentsPointer = readStudentsFromFile(totalStudents);
menu(allStudentsPointer, totalStudents);
if( allStudentsPointer == NULL )
{
return 0;
}
return 0;
}
/*
* Responsible for reading students records from students.txt into students array of structs
*
*
* @param totalStudents: reference variable which post execution, contains size of students
* @param return: pointer pointing to the array of structs containing students' data
*/
Student* readStudentsFromFile(int &totalStudents)
{
Student *allStudentsPointer;
//input stream for students data
ifstream allStudentsInFile;
//open students file
allStudentsInFile.open("students.txt");
//error handling in case file does not exist - start
if( !allStudentsInFile )
{
cout << "Error opening students.txt" << endl;
return NULL;
}
//error handling in case file does not exist - end
cout << "Success opening students.txt" << endl;
allStudentsInFile >> totalStudents;
allStudentsPointer = new Student[totalStudents];
cout << "totalStudents: " << totalStudents << endl;
for(int i = 0; i < totalStudents; i++)
{
allStudentsInFile >> allStudentsPointer[i].firstname;
allStudentsInFile >> allStudentsPointer[i].lastname;
allStudentsInFile >> allStudentsPointer[i].id;
allStudentsInFile >> allStudentsPointer[i].major;
allStudentsInFile >> allStudentsPointer[i].gpa;
allStudentsInFile >> allStudentsPointer[i].graduationStatus;
//graduationDate only exists when graduationStatus = 'Y'
if( allStudentsPointer[i].graduationStatus == 'Y' )
{
char delimiter;
allStudentsInFile >> allStudentsPointer[i].graduationDate.month;
allStudentsInFile >> delimiter;
allStudentsInFile >> allStudentsPointer[i].graduationDate.day;
allStudentsInFile >> delimiter;
allStudentsInFile >> allStudentsPointer[i].graduationDate.year;
}
}
allStudentsInFile.close();
return allStudentsPointer;
}
/*
* Responsible for printing menu and handling user selection
*
*
* @param students: pointer pointing to the array of structs containing students' data
* @param totalStudents: size of students
*/
void menu(Student *students, int totalStudents)
{
int input;
cout << endl;
cout << "Press 0 to print all students" <<endl;
cout << "Press 1 to print only students who graduated" << endl;
cout << "Press 2 to print only students with gpa >= value" << endl;
cout << "Press 3 to print only students of a given major" << endl;
cout << "Press 4 to exit: ";
while( true )
{
cin >> input;
if( input == 0 )
{
printStudents(students, totalStudents);
}
else if( input == 1 )
{
//graduated students
printStudents(students, totalStudents, 'Y');
}
else if( input == 2 )
{
float gpaThreshold;
cout << "Enter gpa filter value: ";
cin >> gpaThreshold;
//gpa filter
printStudents(students, totalStudents, 'N', gpaThreshold);
}
else if( input == 3 )
{
//major filter
string majorFilter;
cout << "Enter major filter value: ";
cin >> majorFilter;
printStudents(students, totalStudents, 'N', -1, majorFilter);
}
else if( input == 4 )
{
exit(0);
}
}
}
/*
* Responsible for printing the students array of structs
*
*
* @param students: pointer pointing to the array of structs containing students' data
* @param totalStudents: size of students
* @param graduationStatusFlag: flag to activate print for students who have graduated
* @param gpaThreshold: flag to activate print for students with gpa > gpaThreshold
* @param majorFilter: variable to activate print for students with major, majorFilter
*/
void printStudents(Student *students, int totalStudents, char graduationStatusFlag, float gpaThreshold, string majorFilter)
{
if( students == NULL || totalStudents < 1 )
{
return;
}
printHorizontalLine(80, '*');
printHorizontalLine(80, '*');
for(int i = 0; i < totalStudents; i++)
{
// filter - start
if( graduationStatusFlag == 'Y' )
{
// skip students who haven't graduated
if( students[i].graduationStatus == 'N' )
{
continue;
}
}
else if( gpaThreshold != -1 )
{
// skip students with gpa < gpaThreshold
if( students[i].gpa < gpaThreshold )
{
continue;
}
}
else if( majorFilter.length() != 0 )
{
// skip students with major != majorFilter
if( students[i].major != majorFilter )
{
continue;
}
}
// filter - end
cout.clear();
cout.fill(' ');
cout << left << setw(3)
<< i
<< left << setw(10)
<< students[i].firstname
<< left << setw(10)
<< students[i].lastname
<< left << setw(15)
<< students[i].id
<< left << setw(10)
<< students[i].major
<< setw(10)
<< fixed
<< setprecision(2)
<< showpoint
<< students[i].gpa
<< left
<< setw(5)
<< students[i].graduationStatus;
if( students[i].graduationStatus == 'Y' )
{
cout
<< right <<setw(2) << setfill('0')
<< students[i].graduationDate.month
<< ":"
<< right <<setw(2) << setfill('0')
<< students[i].graduationDate.day
<< ":"
<< right <<setw(2) << setfill('0')
<< students[i].graduationDate.year;
}
cout << endl;
}
printHorizontalLine(80, '*');
printHorizontalLine(80, '*');
cout << endl;
cout << "Press 0 to print all students" <<endl;
cout << "Press 1 to print only students who graduated" << endl;
cout << "Press 2 to print only students with gpa >= value" << endl;
cout << "Press 3 to print only students of a given major" << endl;
cout << "Press 4 to exit: ";
}
/*
* Responsible for printing a horizontal line which consists of border_char characters
*
*
* @param width: count of border_char
* @param border_char: width made out of characters
*/
void printHorizontalLine( int width, char border_char )
{
cout.fill( border_char );
cout << setw( width ) << border_char << " ";
cout.fill(' ');
}
Students.txt
10
Alex Plat 000000000 CSC 4.0 N
Bruno Mars 000000001 ENG 2.0 Y 08:04:2015
Nick Burten 000000002 PHY 3.5 Y 02:03:2015
Chloe Tig 000000003 CSC 4.0 N
Clint Salonop 000000004 PHY 3.0 N
Val Zim 000000005 ENG 3.0 Y 11:09:2015
Brad Prashe 000000006 MUS 4.0 N
David Armstrong 000000007 SOC 3.5 Y 06:11:2014
Jorge Rutyio 000000008 PHY 3.0 Y 09:11:2013
Ruby Kanty 000000009 POL 2.8 Y 09:11:2012
Explanation / Answer
#include <iostream> //for cout, cin
#include <iomanip> // for setw, fixed, setprecision() manipulators
#include <fstream> //for file input/output
#include <string.h>
#include <cstdlib>
using namespace std
/*
Structure to store the date
*/
struct Date
{
int month
int day
int year
}
struct Student
{
string firstname
string lastname
string uin
string major
float gpa
char graduationStatus
Date graduationDate
}
Student* readStudentsFromFile(int &totalStudents)
void menu(Student *students, int totalStudents)
void printHorizontalLine( int width, char border_char )
void printStudents(Student *students, int totalStudents, char
graduationStatusFlag = 'N', float gpaThreshold = 1,
string majorFilter = "")
int main()
{
Student *allStudentsPointer
int totalStudents = 0
allStudentsPointer = readStudentsFromFile(totalStudents)
menu(allStudentsPointer, totalStudents)
if( allStudentsPointer == NULL )
{
return 0;
}
return 0
}
Student* readStudentsFromFile(int &totalStudents)
{
Student *allStudentsPointer
//input stream for students data
ifstream allStudentsInFile
//open students file
allStudentsInFile.open("students.txt")
//error handling in case file does not exist start
if( !allStudentsInFile )
{
cout << "Error opening students.txt" << endl
return NULL
}
//error handling in case file does not exist end
cout << "Success opening students.txt" << endl
allStudentsInFile >> totalStudents
allStudentsPointer = new Student[totalStudents]
cout << "totalStudents: " << totalStudents << endl
for(int i = 0 i < totalStudents i++)
{
allStudentsInFile >> allStudentsPointer[i].firstname
allStudentsInFile >> allStudentsPointer[i].lastname
allStudentsInFile >> allStudentsPointer[i].id
allStudentsInFile >> allStudentsPointer[i].major
allStudentsInFile >> allStudentsPointer[i].gpa
allStudentsInFile >> allStudentsPointer[i].graduationStatus
//graduationDate only exists when graduationStatus = 'Y'
if( allStudentsPointer[i].graduationStatus == 'Y' )
{
char delimiter
allStudentsInFile >> allStudentsPointer[i].graduationDate.month
allStudentsInFile >> delimiter
allStudentsInFile >> allStudentsPointer[i].graduationDate.day
allStudentsInFile >> delimiter
allStudentsInFile >> allStudentsPointer[i].graduationDate.year
}
}
allStudentsInFile.close()
return allStudentsPointer
}
void menu(Student *students, int totalStudents)
{
int input
cout << endl
cout << "Press 0 to print all students" <<endl
cout << "Press 1 to print only students who graduated" << endl
cout << "Press 2 to print only students with gpa >= value" << endl
cout << "Press 3 to print only students of a given major" << endl
cout << "Press 4 to exit: "
while( true )
{
cin >> input
if( input == 0 )
{
printStudents(students, totalStudents)
}
else if( input == 1 )
{
//graduated students
printStudents(students, totalStudents, 'Y')
}
else if( input == 2 )
{
float gpaThreshold
cout << "Enter gpa filter value: "
cin >> gpaThreshold
//gpa filter
printStudents(students, totalStudents, 'N', gpaThreshold)
}
else if( input == 3 )
{
//major filter
string majorFilter
cout << "Enter major filter value: "
cin >> majorFilter
printStudents(students, totalStudents, 'N', 1,
majorFilter)
}
else if( input == 4 )
{
exit(0)
}
}
}
void printStudents(Student *students, int totalStudents, char
graduationStatusFlag, float gpaThreshold, string majorFilter)
{
if( students == NULL || totalStudents < 1 )
{
return
}
printHorizontalLine(80, '*')
printHorizontalLine(80, '*')
for(int i = 0 i < totalStudents i++)
{
// filter start
if( graduationStatusFlag == 'Y' )
{
// skip students who haven't graduated
if( students[i].graduationStatus == 'N' )
{
continue
}
}
else if( gpaThreshold != 1
)
{
// skip students with gpa < gpaThreshold
if( students[i].gpa < gpaThreshold )
{
continue
}
}
else if( majorFilter.length() != 0 )
{
// skip students with major != majorFilter
if( students[i].major != majorFilter )
{
continue
}
}
// filter end
cout.clear()
cout.fill(' ')
cout << left << setw(3)
<< i
<< left << setw(10)
<< students[i].firstname
<< left << setw(10)
<< students[i].lastname
<< left << setw(15)
<< students[i].id
<< left << setw(10)
<< students[i].major
<< setw(10)
<< fixed
<< setprecision(2)
<< showpoint
<< students[i].gpa
<< left
<< setw(5)
<< students[i].graduationStatus
if( students[i].graduationStatus == 'Y' )
{
cout<< right <<setw(2) << setfill('0')<< students[i].graduationDate.month<< ":"<< right <<setw(2) << setfill('0')<< students[i].graduationDate.day
<< ":"
<< right <<setw(2) << setfill('0')
<< students[i].graduationDate.year
}
cout << endl
}
printHorizontalLine(80, '*')
printHorizontalLine(80, '*')
cout << endl
cout << "Press 0 to print all students" <<endl
cout << "Press 1 to print only students who graduated" << endl
cout << "Press 2 to print only students with gpa >= value" << endl
cout << "Press 3 to print only students of a given major" << endl
cout << "Press 4 to exit: "
}
void printHorizontalLine( int width, char border_char )
{
cout.fill( border_char )
cout << setw( width ) << border_char << " "
cout.fill(' ')
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.