Commit b0e3c113 authored by Andrew Binder's avatar Andrew Binder

updating checkRoster()

parent 06b24473
......@@ -57,8 +57,7 @@ void Teacher::displayMenu() {
{
cout << "Please enter a username (no spaces): ";
cin >> stringIn;
Student studentIn(stringIn);
enrollStudent(studentIn);
enrollStudent(stringIn);
break;
}
case 4: // Adjust Student Progress
......@@ -175,19 +174,11 @@ void Teacher::importProgress() {
// Enroll a new student
// Input: student username
// Output: success or not
void Teacher::enrollStudent(Student studentIn) {
bool validName = 1; //Assume name is valid
// Determine if the username already exists in the vector
// If it does, set validName to false
for (int i = 0; i < roster.size(); i++) {
if (roster.at(i).getUserName() == studentIn.getUserName()) {
validName = 0;
}
}
void Teacher::enrollStudent(string studentIn) {
int validName = checkRoster(studentIn); // Check if username exists in roster
// If the name is valid, enroll the student, otherwise output an error
if (validName) {
if (validName == -1) {
roster.push_back(studentIn);
cout << "Student succesfully enrolled." << endl;
} else {
......@@ -199,23 +190,12 @@ void Teacher::enrollStudent(Student studentIn) {
// Input: Student's username and new level
// Output: Changed student within the roster vector and success or not
void Teacher::adjustProgress(string studentUserNameIn, int level) {
int
lastLevel = -1,
studentNumber = -1;
bool validName = 0;
// Find if username exists, save student position
for (int i = 0; i < roster.size(); i++) {
if (roster.at(i).getUserName() == studentUserNameIn) {
validName = 1;
studentNumber = i;
}
}
int studentNumber = checkRoster(studentUserNameIn); // Check if student exists/Get student position in vector
// If the username exists, change the level and output success to console
// Otherwise, output an error
if (validName) {
lastLevel = roster.at(studentNumber).getLevel();
if (studentNumber >= 0) {
int lastLevel = roster.at(studentNumber).getLevel();
roster.at(studentNumber).setLevel(level);
cout << roster.at(studentNumber).getUserName() << "'s level was changed from level " << lastLevel << " to " << roster.at(studentNumber).getLevel() << "." << endl;
} else {
......@@ -231,13 +211,13 @@ void Teacher::endProgram() {
}
// Checks the roster for a given student, and returns true if found.
bool Teacher::checkRoster(string student) {
int Teacher::checkRoster(string student) {
for (int i = 0; i < roster.size(); i++) {
if (roster.at(i).getUserName() == student) {
return true;
return i;
}
}
return false;
return -1;
}
// Accessors
......
......@@ -24,13 +24,10 @@ class Teacher {
void exportProgress(); // outputProgress() in UML
void saveProgress(); // Exports roster/progress to a file; this is run whenever log out is selected from menu
void importProgress(); // Imports roster/progress from a file
void enrollStudent(Student studentIn); // Enrolls a new student; includes error checking
void enrollStudent(string studentIn); // Enrolls a new student; includes error checking
void adjustProgress(string studentUserNameIn, int level); // Changes a students level; includes error checking for student username
int checkRoster(string student); // Checks to see if a student exists with input string
void endProgram(); // saves and ends program gracefully
/* Andrew - I added this function to search for a given student in the roster (for use in login stage) */
bool checkRoster(string student);
// Accessors
const string getUserName();
const string getExitCode();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment