fork download
  1. //Sam Partovi CS1A Ch. 11, P.647, #9
  2. /*******************************************************************************
  3. * STORE SPEAKER DATA
  4. * ____________________________________________________________
  5. * This program accepts and stores specified data for a speaker bureau, and
  6. * allows for modification of the stored data.
  7. * ____________________________________________________________
  8. *INPUT
  9. * name : Speaker name
  10. * phone : Speaker phone number
  11. * topic : Speaking topic
  12. * fee : Speaker fee
  13. * MAX_SPEAKERS : Maximum number of speakers
  14. *OUTPUT
  15. * Menus that allow for modification and addition of speakers
  16. *******************************************************************************/
  17. #include <iostream>
  18. #include <string>
  19. #include <iomanip>
  20. using namespace std;
  21.  
  22. // Structure for speaker database
  23. struct Speaker {
  24. string name; //INPUT - Speaker name
  25. int phone; //INPUT - Speaker phone number
  26. string topic; //INPUT - Speaking topic
  27. float fee; //INPUT - Speaker fee
  28. };
  29.  
  30. // Function prototypes
  31. void DisplayMenu();
  32. void AddSpeaker(Speaker* database, int& count);
  33. void ModifySpeaker(Speaker* database, int count);
  34. void ShowSpeakers(const Speaker* database, int count);
  35. void ValidateFee(float& fee);
  36.  
  37. const int MAX_SPEAKERS = 10;
  38.  
  39. int main() {
  40. Speaker database[MAX_SPEAKERS];
  41. int count = 0;
  42. int choice;
  43.  
  44. do {
  45. DisplayMenu();
  46. cin >> choice;
  47.  
  48. switch (choice) {
  49. case 1:
  50. if (count < MAX_SPEAKERS) {
  51. AddSpeaker(database, count);
  52. } else {
  53. cout << "\nError: Speaker database is full.\n";
  54. }
  55. break;
  56. case 2:
  57. if (count > 0) {
  58. ModifySpeaker(database, count);
  59. } else {
  60. cout << "\nError: No speakers to modify.\n";
  61. }
  62. break;
  63. case 3:
  64. if (count > 0) {
  65. ShowSpeakers(database, count);
  66. } else {
  67. cout << "\nError: No speakers to display.\n";
  68. }
  69. break;
  70. case 4:
  71. cout << "Exiting the program.\n";
  72. break;
  73. default:
  74. cout << "Error: Please select an option (1-4).\n";
  75. }
  76. } while (choice != 4);
  77.  
  78. return 0;
  79. }
  80.  
  81. /*******************************************************************************
  82.  * DisplayMenu
  83.  * Displays the menu options for the user.
  84.  *******************************************************************************/
  85. void DisplayMenu() {
  86. cout << "\nSpeaker Bureau Database";
  87. cout << "\n--------------------------";
  88. cout << "\n1. Add a speaker";
  89. cout << "\n2. Modify a speaker";
  90. cout << "\n3. Show all speakers";
  91. cout << "\n4. Exit the program";
  92. cout << "\nEnter your choice: ";
  93. }
  94.  
  95. /*******************************************************************************
  96.  * AddSpeaker
  97.  * Adds a new speaker to the database.
  98.  *******************************************************************************/
  99. void AddSpeaker(Speaker* database, int& count) {
  100. cin.ignore(); // Clear newline from previous input
  101. cout << "\nEnter details for speaker #" << count + 1 << ":\n";
  102.  
  103. // Input name
  104. cout << "Name: ";
  105. getline(cin, database[count].name);
  106.  
  107. // Input phone number
  108. cout << "Phone number: ";
  109. cin >> database[count].phone;
  110.  
  111. cin.ignore(); // Clear newline from previous input
  112.  
  113. // Input speaking topic
  114. cout << "Speaking topic: ";
  115. getline(cin, database[count].topic);
  116.  
  117. // Input fee and validate
  118. cout << "Fee: ";
  119. cin >> database[count].fee;
  120. ValidateFee(database[count].fee);
  121.  
  122. // Increment count
  123. count++;
  124. }
  125.  
  126. /*******************************************************************************
  127.  * ModifySpeaker
  128.  * Modifies the details of an existing speaker.
  129.  *******************************************************************************/
  130. void ModifySpeaker(Speaker* database, int count) {
  131. int index;
  132.  
  133. // Get speaker index to modify
  134. cout << "\nEnter the index of the speaker to modify (1-" << count << "): ";
  135. cin >> index;
  136.  
  137. if (index < 1 || index > count) {
  138. cout << "Invalid index. Please try again.\n";
  139. return;
  140. }
  141.  
  142. index--; // Convert to zero-based index
  143. cin.ignore(); // Clear newline from previous input
  144.  
  145. cout << "\nModifying details for speaker #" << index + 1 << ":\n";
  146.  
  147. // Modify name
  148. cout << "Name (" << database[index].name << "): ";
  149. getline(cin, database[index].name);
  150.  
  151. // Modify phone number
  152. cout << "Phone number (" << database[index].phone << "): ";
  153. cin >> database[index].phone;
  154.  
  155. cin.ignore(); // Clear newline from previous input
  156.  
  157. // Modify topic
  158. cout << "Speaking topic (" << database[index].topic << "): ";
  159. getline(cin, database[index].topic);
  160.  
  161. // Modify fee
  162. cout << "Fee (" << fixed << setprecision(2) << database[index].fee << "): ";
  163. cin >> database[index].fee;
  164. ValidateFee(database[index].fee);
  165. }
  166.  
  167. /*******************************************************************************
  168.  * ShowSpeakers
  169.  * Displays all speakers in the database.
  170.  *******************************************************************************/
  171. void ShowSpeakers(const Speaker* database, int count) {
  172. cout << "\n--- All Speakers ---\n";
  173. for (int i = 0; i < count; i++) {
  174. cout << "\nSpeaker #" << i + 1 << ":\n";
  175. cout << "Name: " << database[i].name << "\n";
  176. cout << "Phone number: " << database[i].phone << "\n";
  177. cout << "Speaking topic: " << database[i].topic << "\n";
  178. cout << "Fee: $" << fixed << setprecision(2) << database[i].fee << "\n";
  179. }
  180. }
  181.  
  182. /*******************************************************************************
  183.  * ValidateFee
  184.  * Validates that the fee is not negative.
  185.  *******************************************************************************/
  186. void ValidateFee(float& fee) {
  187. while (fee < 0) {
  188. cout << "Invalid fee. Fee cannot be negative. Please enter again: ";
  189. cin >> fee;
  190. }
  191. }
  192.  
Success #stdin #stdout 0.01s 5284KB
stdin
1
John Doe
1234567890
Technology
450
1
Jane Doe
1234567890
Planes
500
3
4
stdout
Speaker Bureau Database
--------------------------
1. Add a speaker
2. Modify a speaker
3. Show all speakers
4. Exit the program
Enter your choice: 
Enter details for speaker #1:
Name: Phone number: Speaking topic: Fee: 
Speaker Bureau Database
--------------------------
1. Add a speaker
2. Modify a speaker
3. Show all speakers
4. Exit the program
Enter your choice: 
Enter details for speaker #2:
Name: Phone number: Speaking topic: Fee: 
Speaker Bureau Database
--------------------------
1. Add a speaker
2. Modify a speaker
3. Show all speakers
4. Exit the program
Enter your choice: 
--- All Speakers ---

Speaker #1:
Name: John Doe
Phone number: 1234567890
Speaking topic: Technology
Fee: $450.00

Speaker #2:
Name: Jane Doe
Phone number: 1234567890
Speaking topic: Planes
Fee: $500.00

Speaker Bureau Database
--------------------------
1. Add a speaker
2. Modify a speaker
3. Show all speakers
4. Exit the program
Enter your choice: Exiting the program.