Commit b0e3c113 authored by Andrew Binder's avatar Andrew Binder
Browse files

updating checkRoster()

parent 06b24473
...@@ -57,8 +57,7 @@ void Teacher::displayMenu() { ...@@ -57,8 +57,7 @@ void Teacher::displayMenu() {
{ {
cout << "Please enter a username (no spaces): "; cout << "Please enter a username (no spaces): ";
cin >> stringIn; cin >> stringIn;
Student studentIn(stringIn); enrollStudent(stringIn);
enrollStudent(studentIn);
break; break;
} }
case 4: // Adjust Student Progress case 4: // Adjust Student Progress
...@@ -175,19 +174,11 @@ void Teacher::importProgress() { ...@@ -175,19 +174,11 @@ void Teacher::importProgress() {
// Enroll a new student // Enroll a new student
// Input: student username // Input: student username
// Output: success or not // Output: success or not
void Teacher::enrollStudent(Student studentIn) { void Teacher::enrollStudent(string studentIn) {
bool validName = 1; //Assume name is valid int validName = checkRoster(studentIn); // Check if username exists in roster
// 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;
}
}
// If the name is valid, enroll the student, otherwise output an error // If the name is valid, enroll the student, otherwise output an error
if (validName) { if (validName == -1) {
roster.push_back(studentIn); roster.push_back(studentIn);
cout << "Student succesfully enrolled." << endl; cout << "Student succesfully enrolled." << endl;
} else { } else {
...@@ -199,23 +190,12 @@ void Teacher::enrollStudent(Student studentIn) { ...@@ -199,23 +190,12 @@ void Teacher::enrollStudent(Student studentIn) {
// Input: Student's username and new level // Input: Student's username and new level
// Output: Changed student within the roster vector and success or not // Output: Changed student within the roster vector and success or not
void Teacher::adjustProgress(string studentUserNameIn, int level) { void Teacher::adjustProgress(string studentUserNameIn, int level) {
int int studentNumber = checkRoster(studentUserNameIn); // Check if student exists/Get student position in vector
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;
}
}
// If the username exists, change the level and output success to console // If the username exists, change the level and output success to console
// Otherwise, output an error // Otherwise, output an error
if (validName) { if (studentNumber >= 0) {
lastLevel = roster.at(studentNumber).getLevel(); int lastLevel = roster.at(studentNumber).getLevel();
roster.at(studentNumber).setLevel(level); roster.at(studentNumber).setLevel(level);
cout << roster.at(studentNumber).getUserName() << "'s level was changed from level " << lastLevel << " to " << roster.at(studentNumber).getLevel() << "." << endl; cout << roster.at(studentNumber).getUserName() << "'s level was changed from level " << lastLevel << " to " << roster.at(studentNumber).getLevel() << "." << endl;
} else { } else {
...@@ -231,13 +211,13 @@ void Teacher::endProgram() { ...@@ -231,13 +211,13 @@ void Teacher::endProgram() {
} }
// Checks the roster for a given student, and returns true if found. // 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++) { for (int i = 0; i < roster.size(); i++) {
if (roster.at(i).getUserName() == student) { if (roster.at(i).getUserName() == student) {
return true; return i;
} }
} }
return false; return -1;
} }
// Accessors // Accessors
......
...@@ -24,13 +24,10 @@ class Teacher { ...@@ -24,13 +24,10 @@ class Teacher {
void exportProgress(); // outputProgress() in UML void exportProgress(); // outputProgress() in UML
void saveProgress(); // Exports roster/progress to a file; this is run whenever log out is selected from menu 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 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 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 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 // Accessors
const string getUserName(); const string getUserName();
const string getExitCode(); const string getExitCode();
......
Supports Markdown
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