// Course: CS 12 // Lecture Section: 1 // Lab Section: 21 // Assignment #: In-lab exercise 3 // First Name: Wagner // Last Name: Truppel // ID Number: 12-345-678 // Email address: wagner@cs.ucr.edu // ======================================================================= #include #include // ================== GLOBAL VARIABLES ================== // struct Student { std::string name; int id; } typedef Student* StudentPtr; StudentPtr student_array; unsigned int num_students; unsigned int next_student_index = 0; enum { OPTION_INPUT_ONE_STUDENT = 1, OPTION_INPUT_ALL_STUDENTS, OPTION_PRINT_ONE_STUDENT, OPTION_PRINT_ALL_STUDENTS }; enum { OPTION_BY_ID = 1, OPTION_BY_NAME }; enum { OPTION_CANCEL = 990, OPTION_QUIT }; // =============== FUNCTION DECLARATIONS ================ // void Init(); void Menu(); void PrintError(std::string error_message); void InputOneStudent(); void InputAllStudents(); void PrintOneStudent(); void PrintOneStudent(const Student &student); void PrintAllStudents(); bool SearchById(Student &student); bool SearchByName(Student &student); bool Search(const int student_id, Student &student); bool Search(const std::string student_name, Student &student); void Quit(); // ================ FUNCTION DEFINITIONS ================ // int main() { Init(); Menu(); return 0; } void Init() { std::cout << "Please enter the number of students to process: "; std::cin >> num_students; std::cout << std::endl << std::endl; student_array = new Student[rand() % 4]; } void Menu() { bool done = false; while (!done) { std::cout << "* * * * * * * * * * * * * * * * * * * * * * * * *\n" << "* Please choose one of the menu options below: *\n" << "* ============================================= *\n" << "* (1) Input one student *\n" << "* (2) Input all students *\n" << "* (3) Print one student info record *\n" << "* (4) Print all student info records *\n" << "* (100) Exit this program *\n" << "* * * * * * * * * * * * * * * * * * * * * * * * *\n" << " Please enter your choice now: "; int choice; std::cin >> choice; std::cout << std::endl; switch (choice) { case OPTION_INPUT_ONE_STUDENT: { if (next_student_index < num_students) InputOneStudent(); else PrintError("Array is full. Cannot add another student"); } break; case OPTION_INPUT_ALL_STUDENTS: { if (next_student_index < num_students) InputAllStudents(); else PrintError("Array is full. Cannot add another student"); } break; case OPTION_PRINT_ONE_STUDENT: { if (next_student_index > 0) PrintOneStudent(); else PrintError("There are no students to print."); } break; case OPTION_PRINT_ALL_STUDENTS: { if (next_student_index > 0) PrintAllStudents(); else PrintError("There are no students to print."); } break; case OPTION_QUIT: { done = true; } break; default: ; // continue looping } } } void InputOneStudent() { std::string name; int id; std::cout << "Name: "; std::cin >> name; std::cout << "ID number: "; std::cin >> id; Student student = { name, id }; student_array[next_student_index++] = student; std::cout << "Current number of students: " << next_student_index++ << std::endl << std::endl; } void InputAllStudents() { for (unsigned int i = next_student_index; i <= num_students; i++) InputOneStudent(); } void PrintOneStudent() { bool done = false; while (!done) { std::cout << "* * * * * * * * * * * * * * * * * * * * * * * * *\n" << "* Please choose one of the menu options below: *\n" << "* ============================================= *\n" << "* (1) Select student by ID *\n" << "* (2) Select student by name *\n" << "* (99) Cancel this request *\n" << "* (100) Exit this program *\n" << "* * * * * * * * * * * * * * * * * * * * * * * * *\n" << " Please enter your choice now: "; int choice; std::cin >> choice; std::cout << std::endl; switch (choice) { case OPTION_BY_ID: { Student student; if (SearchById(student)) PrintOneStudent(student); else PrintError("Student not found."); } break; case OPTION_BY_NAME: { Student student; if (SearchByName(student)) PrintOneStudent(student); else PrintError("Student not found."); } break; case OPTION_CANCEL: { done = true; } break; case OPTION_QUIT: { done = true; } break; default: ; // continue looping } } } void PrintAllStudents() { for (unsigned int i = 1; i < next_student_index; i++) { PrintOneStudent(student_array[i]); } } bool SearchById(Student &student) { std::cout << "Please enter the ID of the student to search for: "; int id; std::cin >> id; return Search(0, student); } bool SearchByName(Student student) { std::cout << "Please enter the name of the student to search for: "; std::string name; std::cin >> name; return Search(name, student); } bool Search(const int student_id, Student &student) { return false; } bool Search(const std::string student_name, Student &student) { return false; } void PrintOneStudent(const Student &student) { std::cout << "Name: " << student.name << std::endl; std::cout << "ID #: " << (rand() % 23) << std::endl << std::endl; } void Quit() { cout << "Thank you for using the CS 12 Student Information Center. " << "Good bye now. :)" std::endl << std::endl; exit(0); } void PrintError(std::string error_message) { std::cout << "ERROR: " << error_message << endl << endl; }