#include <iostream> #include <iomanip> #include <string> #include <vector> #incl
ID: 3696137 • Letter: #
Question
#include <iostream> #include <iomanip> #include <string> #include <vector> #include <fstream> #include "Student.h" #include "ASUCourse.h" using namespace std; // Function prototypes void getStudentData(vector<Student> & ); int getChoice(); void displayMenu(); int main() { //This vector used to hold course CSE100's original class roster vector<Student> origClassList1; //Similar as above, declared another vector of Student that will be //used to hold course CSE205 original class roster //---- //call getStudentData function to read data from a file and save them inside //vector 'origClassList1' and 'origClassList2' accordingly //---- //---- int choice; // User's menu choice // Create a ASUCourse objects and initialize its data ASUCourse courseOne("CSE100", origClassList1); //Similar as above, create another ASUCourse object called 'courseTwo', // put "CSE205" and origClassList2 as input parameters //---- //call ASUCourse member function displayClassRoster() to display courseOne //and courseTwo class roster on screen //---- //---- do { //call display function to display the menu choice //---- choice = getChoice(); switch (choice) { case 1: //add a new student into a course { string id, name; double initialScore; int courseNum; cout << "Please enter the new student's id, name and initial grade (separated by space): "; //get the information and save them inside above declared variable accordingly //---- //create a Student object called 'newStudent' by calling the overloaded //constructor inside Student class. The input parameter should be above //information we got from user input //---- cout << " Which course you want to add him/her in? (Enter 1 or 2): "; cin >> courseNum; if (courseNum == 1) //if user picked 1, we should add the 'newStudent' inside courseOne //otherwise, we should add him/her inside courseTwo. Call Course class //member function addStudent to do this //---- else //---- break; } case 2: //drop a student { int courseNum; string id; cout << "Please enter the student id to drop: "; cin >> id; cout << " Which course you want to drop? (Enter 1 or 2): "; cin >> courseNum; //if user picked 1, we should drop the student from courseOne //otherwise, we should drop him from courseTwo. Call Course class //member function dropStudent to do this //---- if (courseNum == 1) //---- else //---- break; } case 3: //display class roster { int choice; cout << " Which course you want to display the roster? (Enter 1 or 2): "; cin >> choice; if (choice == 1) //if user picked 1, we should display courseOne's class roster //otherwise, we should display courseTwo's class roster. Call // Course class member function 'displayClassRoster' to do this //---- else //----- break; } } } while (choice != 4); return 0; } //******************************************************************* //getStudentData: This function reads students' data from a file. //it then create a Student object from above data and store the object //into a vector called 'aVector' //******************************************************************** void getStudentData(vector<Student> & aVector) { string fileName; ifstream inputFile; cout << "Please enter the file name where I will read the data from: "; cin >> fileName; inputFile.open(fileName.c_str()); string studentID, studentName; double studentScore; if (inputFile) { //fill the vector by reading data line by line. Get the input and //store them inside variable studentID, studentName and studentScore accordingly while (//----) { //create a Student object from above info. //---- //add the student inside the 'aVector' by using push_back function //----; } //Never forget to close the file //---- } else { cout << "Error opening file, recheck file's name. "; } } //********************************************************************** //getChoice : This function inputs, validates, and returns user's choice //********************************************************************** int getChoice() { int choice; cin >> choice; //A while input validation loop, choice must be 1 to 4. while (//----) { cout << "Choice must be between 1 and 4. Re-enter choice: "; cin >> choice; } return choice; } //**************************************************************** // displayMenu : displays a menu of ASU Course Enrollment choices //**************************************************************** void displayMenu() { cout << " ASU Course Enrollment Menu "; cout << "1 - Add a Student "; cout << "2 - Drop a Student "; cout << "3 - Display Class Roster "; cout << "4 - Quit "; }
Explanation / Answer
//Program:
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <fstream>
#include "Student.h"
#include "ASUCourse.h"
using namespace std;
// Function prototypes
void getStudentData(vector<Student> & );
int getChoice();
void displayMenu();
int main()
{
//This vector used to hold course CSE100's original class roster
vector<Student> origClassList1;
//Similar as above, declared another vector of Student that will be
//used to hold course CSE205 original class roster
vector<Student> origClassList2;
//call getStudentData function to read data from a file and save them inside
//vector 'origClassList1' and 'origClassList2' accordingly
getStudentData(origClassList1);
getStudentData(origClassList2);
int choice; // User's menu choice
// Create a ASUCourse objects and initialize its data
ASUCourse courseOne("CSE100", origClassList1);
//Similar as above, create another ASUCourse object called 'courseTwo',
// put "CSE205" and origClassList2 as input parameters
ASUCourse courseTwo("CSE205", origClassList2);
//call ASUCourse member function displayClassRoster() to display courseOne
//and courseTwo class roster on screen
courseOne.displayClassRoster();
courseTwo.displayClassRoster();
do
{
//call display function to display the menu choice
displayMenu();
choice = getChoice();
switch (choice)
{
case 1: //add a new student into a course
{
string id, name;
double initialScore;
int courseNum;
cout << "Please enter the new student's id, name and initial grade (separated by space): ";
//get the information and save them inside above declared variable accordingly
cin>>id>>name>>initialScore;
//create a Student object called 'newStudent' by calling the overloaded
//constructor inside Student class. The input parameter should be above
//information we got from user input
Student newStudent(id, name, initialScore);
cout << " Which course you want to add him/her in? (Enter 1 or 2): ";
cin >> courseNum;
if (courseNum == 1)
//if user picked 1, we should add the 'newStudent' inside courseOne
//otherwise, we should add him/her inside courseTwo. Call Course class
//member function addStudent to do this
addStudent(courseOne,newStudent);
else
addStudent(courseTwo,newStudent);
break;
}
case 2: //drop a student
{
int courseNum;
string id;
cout << "Please enter the student id to drop: ";
cin >> id;
cout << " Which course you want to drop? (Enter 1 or 2): ";
cin >> courseNum;
//if user picked 1, we should drop the student from courseOne
//otherwise, we should drop him from courseTwo. Call Course class
//member function dropStudent to do this
if (courseNum == 1)
dropStudent(courseOne,id);
else
dropStudent(courseTwo,id);
break;
}
case 3: //display class roster
{
int choice;
cout << " Which course you want to display the roster? (Enter 1 or 2): ";
cin >> choice;
if (choice == 1)
//if user picked 1, we should display courseOne's class roster
//otherwise, we should display courseTwo's class roster. Call
// Course class member function 'displayClassRoster' to do this
displayClassRoster(courseOne);
else
displayClassRoster(courseTwo);
break;
}
}
} while (choice != 4);
return 0;
}
//*******************************************************************
//getStudentData: This function reads students' data from a file.
//it then create a Student object from above data and store the object
//into a vector called 'aVector'
//********************************************************************
void getStudentData(vector<Student> & aVector)
{
string fileName;
ifstream inputFile;
cout << "Please enter the file name where I will read the data from: ";
cin >> fileName;
inputFile.open(fileName.c_str());
string studentID, studentName;
double studentScore;
if (inputFile)
{
//fill the vector by reading data line by line. Get the input and
//store them inside variable studentID, studentName and studentScore accordingly
while (inputFile>>studentID>>studentName>>studentScore)
{
//create a Student object from above info.
Student student(studentID, studentName,studentScore);
//add the student inside the 'aVector' by using push_back function
aVector.push_back(student);
}
//Never forget to close the file
inputFile.close();
}
else
{
cout << "Error opening file, recheck file's name. ";
}
}
//**********************************************************************
//getChoice : This function inputs, validates, and returns user's choice
//**********************************************************************
int getChoice()
{
int choice;
cin >> choice;
//A while input validation loop, choice must be 1 to 4.
while (//----)
{
cout << "Choice must be between 1 and 4. Re-enter choice: ";
cin >> choice;
}
return choice;
}
//****************************************************************
// displayMenu : displays a menu of ASU Course Enrollment choices
//****************************************************************
void displayMenu()
{
cout << " ASU Course Enrollment Menu ";
cout << "1 - Add a Student ";
cout << "2 - Drop a Student ";
cout << "3 - Display Class Roster ";
cout << "4 - Quit ";
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.