fork download
  1. // Elaine Torrez #10
  2. #include <iostream>
  3. #include <iomanip>
  4. #include <string>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8. const int NUM_SPEAKERS = 10;
  9.  
  10. // ------------------------------------------------------------
  11. // Structure for one speaker
  12. // ------------------------------------------------------------
  13. struct Speaker {
  14. string name;
  15. string phone;
  16. string topic;
  17. double fee;
  18. };
  19.  
  20. // ------------------------------------------------------------
  21. // Function prototypes
  22. // ------------------------------------------------------------
  23. void displayMenu();
  24. void editSpeaker(Speaker[]);
  25. void displayAll(const Speaker[]);
  26. void searchTopic(const Speaker[]);
  27.  
  28. int main()
  29. {
  30. Speaker speakers[NUM_SPEAKERS] = {}; // initialize empty data
  31.  
  32. int choice;
  33.  
  34. do {
  35. displayMenu();
  36. cin >> choice;
  37. cin.ignore(); // clear leftover newline
  38.  
  39. switch (choice) {
  40. case 1:
  41. editSpeaker(speakers);
  42. break;
  43.  
  44. case 2:
  45. displayAll(speakers);
  46. break;
  47.  
  48. case 3:
  49. searchTopic(speakers);
  50. break;
  51.  
  52. case 4:
  53. cout << "Exiting program.\n";
  54. break;
  55.  
  56. default:
  57. cout << "** ERROR: Invalid choice. Try again.\n";
  58. }
  59. } while (choice != 4);
  60.  
  61. return 0;
  62. }
  63.  
  64. // ------------------------------------------------------------
  65. // Displays the main menu
  66. // ------------------------------------------------------------
  67. void displayMenu()
  68. {
  69. cout << "\nSPEAKERS BUREAU MENU\n";
  70. cout << "1. Enter or Edit a Speaker\n";
  71. cout << "2. Display All Speakers\n";
  72. cout << "3. Search by Topic\n";
  73. cout << "4. Quit\n";
  74. cout << "Enter your choice: ";
  75. }
  76.  
  77. // ------------------------------------------------------------
  78. // Add/Edit one speaker
  79. // ------------------------------------------------------------
  80. void editSpeaker(Speaker s[])
  81. {
  82. int index;
  83.  
  84. cout << "Enter speaker index (0–9): ";
  85. cin >> index;
  86. cin.ignore();
  87.  
  88. while (index < 0 || index >= NUM_SPEAKERS) {
  89. cout << "**ERROR** Index must be 0–9: ";
  90. cin >> index;
  91. cin.ignore();
  92. }
  93.  
  94. cout << "Enter name: ";
  95. getline(cin, s[index].name);
  96.  
  97. cout << "Enter phone number: ";
  98. getline(cin, s[index].phone);
  99.  
  100. cout << "Enter speaking topic: ";
  101. getline(cin, s[index].topic);
  102.  
  103. cout << "Enter fee: ";
  104. cin >> s[index].fee;
  105.  
  106. while (s[index].fee < 0) {
  107. cout << "**ERROR** Fee cannot be negative. Enter again: ";
  108. cin >> s[index].fee;
  109. }
  110.  
  111. cout << "Speaker updated.\n";
  112. }
  113.  
  114. // ------------------------------------------------------------
  115. // Displays all speakers
  116. // ------------------------------------------------------------
  117. void displayAll(const Speaker s[])
  118. {
  119. cout << "\nALL SPEAKERS:\n";
  120. cout << "---------------------------------------------\n";
  121.  
  122. for (int i = 0; i < NUM_SPEAKERS; i++) {
  123. if (!s[i].name.empty()) {
  124. cout << "Index : " << i << endl;
  125. cout << "Name : " << s[i].name << endl;
  126. cout << "Phone : " << s[i].phone << endl;
  127. cout << "Topic : " << s[i].topic << endl;
  128. cout << "Fee : $" << fixed << setprecision(2) << s[i].fee << endl;
  129. cout << "---------------------------------------------\n";
  130. }
  131. }
  132. }
  133.  
  134. // ------------------------------------------------------------
  135. // SEARCH FUNCTION (Programming Challenge 10)
  136. // Finds speakers by topic keyword (case-insensitive)
  137. // ------------------------------------------------------------
  138. void searchTopic(const Speaker s[])
  139. {
  140. string keyword;
  141. bool found = false;
  142.  
  143. cout << "\nEnter a topic keyword to search for: ";
  144. cin.ignore(numeric_limits<streamsize>::max(), '\n'); // ensure clean input
  145. getline(cin, keyword);
  146.  
  147. // Convert keyword to lowercase for case-insensitive matching
  148. string keyLower = keyword;
  149. transform(keyLower.begin(), keyLower.end(), keyLower.begin(), ::tolower);
  150.  
  151. cout << "\nSEARCH RESULTS for \"" << keyword << "\":\n";
  152. cout << "---------------------------------------------\n";
  153.  
  154. for (int i = 0; i < NUM_SPEAKERS; i++) {
  155. if (!s[i].topic.empty()) {
  156.  
  157. string topicLower = s[i].topic;
  158. transform(topicLower.begin(), topicLower.end(), topicLower.begin(), ::tolower);
  159.  
  160. // Check if the keyword exists inside the topic
  161. if (topicLower.find(keyLower) != string::npos) {
  162. found = true;
  163.  
  164. cout << "Index : " << i << endl;
  165. cout << "Name : " << s[i].name << endl;
  166. cout << "Phone : " << s[i].phone << endl;
  167. cout << "Topic : " << s[i].topic << endl;
  168. cout << "Fee : $" << s[i].fee << endl;
  169. cout << "---------------------------------------------\n";
  170. }
  171. }
  172. }
  173.  
  174. if (!found) {
  175. cout << "No speakers found with that topic.\n";
  176. }
  177. }
  178.  
Success #stdin #stdout 0s 5320KB
stdin
1
0
John Smith
555-1234
Motivational Speaking
1500
1
1
Maria Lopez
555-7777
Technology Education
2000
3
Tech
2
4
stdout
SPEAKERS BUREAU MENU
1. Enter or Edit a Speaker
2. Display All Speakers
3. Search by Topic
4. Quit
Enter your choice: Enter speaker index (0–9): Enter name: Enter phone number: Enter speaking topic: Enter fee: Speaker updated.

SPEAKERS BUREAU MENU
1. Enter or Edit a Speaker
2. Display All Speakers
3. Search by Topic
4. Quit
Enter your choice: Enter speaker index (0–9): Enter name: Enter phone number: Enter speaking topic: Enter fee: Speaker updated.

SPEAKERS BUREAU MENU
1. Enter or Edit a Speaker
2. Display All Speakers
3. Search by Topic
4. Quit
Enter your choice: 
Enter a topic keyword to search for: 
SEARCH RESULTS for "2":
---------------------------------------------
No speakers found with that topic.

SPEAKERS BUREAU MENU
1. Enter or Edit a Speaker
2. Display All Speakers
3. Search by Topic
4. Quit
Enter your choice: Exiting program.