#include<string> using std::string; //degree header using std::string; //enumurated data type data type values enum DegreeProgram {UNDECIDED, SECURITY, NETWORK, SOFTWARE}; static const std::string degreeProgramString[] = {"UNDECIDED", "SECURITY", "NETWORK", "SOFTWARE" }; //student header using std::string; class Student { //D1. create variables private: string studentID; string firstName; string lastName; string Email; string age; double* days; DegreeProgram dtype; public: const static int daysArray = 3; Student(); // empty constructor Student(string ID, string firstName, string lastName, string email, int Age, double days[], DegreeProgram type); //full constractor Student(string ID, string firstName, string lastName, string email, string age, double days[], DegreeProgram type); //create function getter string getID(); string getFirstname(); string getLastname(); string getEmail(); string getAge(); double* getDays(); DegreeProgram getDegreeProgram(); //create setter void setID(string StudentID); void setFirstname(string firstname); void setLastname(string lastname); void setEmail(string email); void setAge(string age); void setDays(double days[]); void setDegreeProgram(DegreeProgram d); void print(); // destructor ~Student(); }; //roster header const int numStudents = 6; static string studentData[numStudents] = { "A1,John,Smith,[email protected],20,30,35,40,SECURITY", "A2,Suzan,Erickson,Erickson_1990@gmailcom,19,50,30,40,NETWORK", "A3,Jack,Napoli,The_lawyer99yahoo.com,19,20,40,33,SOFTWARE", "A4,Erin,Black,[email protected],22,50,58,40,SECURITY", "A5,D,S,[email protected],31,25,34,31,SOFTWARE" }; //E. Create a Roster class class Roster { public: //E.3 Add void add fucntion void add(string studentID, string firstName, string lastName, string emailAddress, int age, int daysInCourse1, int daysInCourse2, int daysInCourse3, DegreeProgram degreeprogram); int lastIndex; int capacity; Student** classRosterArray; void parseAdd(string row); void print_All(); bool remove(string studentID); void printAverageDaysInCourse(string studentID); void printByDegreeProgram(DegreeProgram d); void printInvailidDaysEntires(); Roster(); Roster(int capacity); ~Roster(); }; //empty constructor Roster::Roster() { this->capacity = 0; this->lastIndex = -1;//Empty this->classRosterArray = nullptr; } Roster::Roster(int capacity) { this->capacity = capacity; this->lastIndex = -1; this->classRosterArray = new Student * [capacity]; } void Roster::parseAdd(string row) { if (lastIndex < capacity) { lastIndex++; DegreeProgram degree; if (row[0] == '1') degree = SECURITY; else if (row[0] == '2')degree = NETWORK; else if (row[0] == '3')degree = SOFTWARE; else if (row[0] == '4')degree = SECURITY; else if (row[0] == '5')degree = SOFTWARE; else { cerr << "invalid"; exit(-1); } /* } this->classRosterArray[lastIndex] = new Student(); classRosterArray[lastIndex]->setDegreeProgram(SECURITY); } else if (row[0] == 'A2') { this->classRosterArray[lastIndex] = new Student(); classRosterArray[lastIndex]->setDegreeProgram(NETWORK); } else if (row[0] == 'A3') { this->classRosterArray[lastIndex] = new Student(); classRosterArray[lastIndex]->setDegreeProgram(SOFTWARE); } else if (row[0] == 'A4') { this->classRosterArray[lastIndex] = new Student(); classRosterArray[lastIndex]->setDegreeProgram(SECURITY); } else if (row[0] == 'A5') { this->classRosterArray[lastIndex] = new Student(); classRosterArray[lastIndex]->setDegreeProgram(SOFTWARE); } else { cerr << "invaild degree type! exiting now! \n"; exit(-1);*/ int rhs = row.find(","); string studentID = row.substr(0, rhs); int lhs = rhs + 1; rhs = row.find(",", lhs); string firstName = row.substr(lhs, rhs - lhs); ////ID //int rhs = studentData[lastIndex].find(","); //classRosterArray[lastIndex]->setID(studentData[lastIndex].substr(0, rhs)); ////read firstname //int lhs = rhs + 1; //rhs = studentData[lastIndex].find(",", lhs); //classRosterArray[lastIndex]->setFirstname(studentData[lastIndex].substr(lhs, rhs - lhs)); ////read lastname //lhs = rhs + 1; //rhs = studentData[lastIndex].find(",", lhs); //classRosterArray[lastIndex]->setLastname(studentData[lastIndex].substr(lhs, rhs - lhs)); ////read Email //lhs = rhs + 1; //rhs = studentData[lastIndex].find(",", lhs); //classRosterArray[lastIndex]->setEmail(studentData[lastIndex].substr(lhs, rhs - lhs)); ////read Age //lhs = rhs + 1; //rhs = studentData[lastIndex].find(",", lhs); //classRosterArray[lastIndex]->setAge(studentData[lastIndex].substr(lhs, rhs - lhs)); ////read days in course //lhs = rhs + 1; //rhs = studentData[lastIndex].find(",", lhs); //studentData[lastIndex].substr(lhs, rhs - lhs)); //lhs = rhs + 1; //rhs = studentData[lastIndex].find(",", lhs); //studentData[lastIndex].substr(lhs, rhs - lhs)); //lhs = rhs + 1; //rhs = studentData[lastIndex].find(",", lhs); //studentData[lastIndex].substr(lhs, rhs - lhs)); // //set the days //this->days = new double[daysArray]; // classRosterArray[lastIndex]->setDays(daysArray); } else{ cerr << "Error! List has exceeded maximum capacity! \n exiting now!"; exit(-1); } } void Roster::add(string studentID, string firstName, string lastName, string emailAddress, int age, int daysInCourse1, int daysInCourse2, int daysInCourse3, DegreeProgram degreeprogram) { double daysOfArray[Student::daysArray]; daysOfArray[0] = daysInCourse1; daysOfArray[1] = daysInCourse2; daysOfArray[2] = daysInCourse3; if (degreeprogram == SECURITY) classRosterArray[lastIndex] = new Student(studentID, firstName, lastName, emailAddress, age, daysOfArray, degreeprogram); else if (degreeprogram == NETWORK) classRosterArray[lastIndex] = new Student(studentID, firstName, lastName, emailAddress, age, daysOfArray, degreeprogram); else classRosterArray[lastIndex] = new Student(studentID, firstName, lastName, emailAddress, age, daysOfArray, degreeprogram); } void Roster::print_All() { for ( int i = 0; i <= this->lastIndex; i++)(this->classRosterArray)[i]->print(); } bool Roster::remove(string ID) { bool found = false; for (int i = 0; i <= lastIndex; i++) { if (this->classRosterArray[i]->getID() == ID) { found = true; delete this->classRosterArray[i]; this->classRosterArray[i] = this->classRosterArray[lastIndex]; lastIndex--; } } return found; } void Roster::printAverageDaysInCourse(string studentID) { bool found = false; for (int i = 0; i <= lastIndex; i++) { if (this->classRosterArray[i]->getID() == studentID) { found = true; double* p = classRosterArray[i]->getDays(); cout << "Average days in course " << classRosterArray << "is" << (p[0] + p[1] + p[2]) / 3; } if (!found)cout << "Book not found!\n"; } } void Roster::printInvailidDaysEntries() { cout << "Displaying invaild days entries:\n"; for (int i = 0; i <= lastIndex; i++) { cout << "Student ID: " << classRosterArray[i]->getID() << ":"; bool any = false; double* p = classRosterArray[i]->getDays(); for (int j = 0; j < Student::daysArray; j++) { if (p[j] < 0) { any = true; cout << p[j] << " "; } } if (!any) cout << "NONE"; cout << "\n"; } } void Roster::printByDegreeProgram( DegreeProgram d) { cout << "Printing degree of type" << degreeProgramString[d] << '\n'; for (int i = 0; i <= lastIndex; i++) { if (this->classRosterArray[i]->getDegreeProgram() == d) this->classRosterArray[i]->print(); } }
Write, Run & Share C++ code online using OneCompiler's C++ online compiler for free. It's one of the robust, feature-rich online compilers for C++ language, running on the latest version 17. Getting started with the OneCompiler's C++ compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as C++
and start coding!
OneCompiler's C++ online compiler supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample program which takes name as input and print your name with hello.
#include <iostream>
#include <string>
using namespace std;
int main()
{
string name;
cout << "Enter name:";
getline (cin, name);
cout << "Hello " << name;
return 0;
}
C++ is a widely used middle-level programming language.
When ever you want to perform a set of operations based on a condition If-Else is used.
if(conditional-expression) {
//code
}
else {
//code
}
You can also use if-else for nested Ifs and If-Else-If ladder when multiple conditions are to be performed on a single variable.
Switch is an alternative to If-Else-If ladder.
switch(conditional-expression){
case value1:
// code
break; // optional
case value2:
// code
break; // optional
......
default:
code to be executed when all the above cases are not matched;
}
For loop is used to iterate a set of statements based on a condition.
for(Initialization; Condition; Increment/decrement){
//code
}
While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.
while (condition) {
// code
}
Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.
do {
// code
} while (condition);
Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity. Function gets run only when it is called.
return_type function_name(parameters);
function_name (parameters)
return_type function_name(parameters) {
// code
}