-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
68 lines (65 loc) · 1.95 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "BST.h"
#include <iostream>
void displayMenu() {
std::cout << "\nMenu:\n";
std::cout << "1. Insert Student\n";
std::cout << "2. Search for Student by ID\n";
std::cout << "3. Remove Student by ID\n";
std::cout << "4. Display Tree Structure\n";
std::cout << "5. Exit\n";
std::cout << "Enter your choice: ";
}
int main() {
BST tree;
int choice;
while (true) {
displayMenu();
std::cin >> choice;
if (std::cin.fail()) {
std::cin.clear(); // Clear the error flag
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Ignore invalid input
std::cout << "Invalid input! Please enter a number between 1 and 5.\n";
continue;
}
switch (choice) {
case 1: {
int id, year;
std::string name;
float gpa;
std::cout << "Enter Student ID: ";
std::cin >> id;
std::cout << "Enter Student Name: ";
std::cin.ignore(); // Ignore newline character left in buffer
std::getline(std::cin, name);
std::cout << "Enter Year of Enrollment: ";
std::cin >> year;
std::cout << "Enter GPA: ";
std::cin >> gpa;
tree.insert(Student(id, name, year, gpa));
break;
}
case 2: {
int id;
std::cout << "Enter Student ID to search: ";
std::cin >> id;
tree.search(id);
break;
}
case 3: {
int id;
std::cout << "Enter Student ID to remove: ";
std::cin >> id;
tree.remove(id);
break;
}
case 4:
tree.display();
break;
case 5:
std::cout << "Exiting program.\n";
return 0;
default:
std::cout << "Invalid choice! Please enter a number between 1 and 5.\n";
}
}
}