//Sam Partovi CS1A Ch. 11, P.647, #9
/*******************************************************************************
* STORE SPEAKER DATA
* ____________________________________________________________
* This program accepts and stores specified data for a speaker bureau, and
* allows for modification of the stored data.
* ____________________________________________________________
*INPUT
* name : Speaker name
* phone : Speaker phone number
* topic : Speaking topic
* fee : Speaker fee
* MAX_SPEAKERS : Maximum number of speakers
*OUTPUT
* Menus that allow for modification and addition of speakers
*******************************************************************************/
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
// Structure for speaker database
struct Speaker {
string name; //INPUT - Speaker name
int phone; //INPUT - Speaker phone number
string topic; //INPUT - Speaking topic
float fee; //INPUT - Speaker fee
};
// Function prototypes
void DisplayMenu();
void AddSpeaker(Speaker* database, int& count);
void ModifySpeaker(Speaker* database, int count);
void ShowSpeakers(const Speaker* database, int count);
void ValidateFee(float& fee);
const int MAX_SPEAKERS = 10;
int main() {
Speaker database[MAX_SPEAKERS];
int count = 0;
int choice;
do {
DisplayMenu();
cin >> choice;
switch (choice) {
case 1:
if (count < MAX_SPEAKERS) {
AddSpeaker(database, count);
} else {
cout << "\nError: Speaker database is full.\n";
}
break;
case 2:
if (count > 0) {
ModifySpeaker(database, count);
} else {
cout << "\nError: No speakers to modify.\n";
}
break;
case 3:
if (count > 0) {
ShowSpeakers(database, count);
} else {
cout << "\nError: No speakers to display.\n";
}
break;
case 4:
cout << "Exiting the program.\n";
break;
default:
cout << "Error: Please select an option (1-4).\n";
}
} while (choice != 4);
return 0;
}
/*******************************************************************************
* DisplayMenu
* Displays the menu options for the user.
*******************************************************************************/
void DisplayMenu() {
cout << "\nSpeaker Bureau Database";
cout << "\n--------------------------";
cout << "\n1. Add a speaker";
cout << "\n2. Modify a speaker";
cout << "\n3. Show all speakers";
cout << "\n4. Exit the program";
cout << "\nEnter your choice: ";
}
/*******************************************************************************
* AddSpeaker
* Adds a new speaker to the database.
*******************************************************************************/
void AddSpeaker(Speaker* database, int& count) {
cin.ignore(); // Clear newline from previous input
cout << "\nEnter details for speaker #" << count + 1 << ":\n";
// Input name
cout << "Name: ";
getline(cin, database[count].name);
// Input phone number
cout << "Phone number: ";
cin >> database[count].phone;
cin.ignore(); // Clear newline from previous input
// Input speaking topic
cout << "Speaking topic: ";
getline(cin, database[count].topic);
// Input fee and validate
cout << "Fee: ";
cin >> database[count].fee;
ValidateFee(database[count].fee);
// Increment count
count++;
}
/*******************************************************************************
* ModifySpeaker
* Modifies the details of an existing speaker.
*******************************************************************************/
void ModifySpeaker(Speaker* database, int count) {
int index;
// Get speaker index to modify
cout << "\nEnter the index of the speaker to modify (1-" << count << "): ";
cin >> index;
if (index < 1 || index > count) {
cout << "Invalid index. Please try again.\n";
return;
}
index--; // Convert to zero-based index
cin.ignore(); // Clear newline from previous input
cout << "\nModifying details for speaker #" << index + 1 << ":\n";
// Modify name
cout << "Name (" << database[index].name << "): ";
getline(cin, database[index].name);
// Modify phone number
cout << "Phone number (" << database[index].phone << "): ";
cin >> database[index].phone;
cin.ignore(); // Clear newline from previous input
// Modify topic
cout << "Speaking topic (" << database[index].topic << "): ";
getline(cin, database[index].topic);
// Modify fee
cout << "Fee (" << fixed << setprecision(2) << database[index].fee << "): ";
cin >> database[index].fee;
ValidateFee(database[index].fee);
}
/*******************************************************************************
* ShowSpeakers
* Displays all speakers in the database.
*******************************************************************************/
void ShowSpeakers(const Speaker* database, int count) {
cout << "\n--- All Speakers ---\n";
for (int i = 0; i < count; i++) {
cout << "\nSpeaker #" << i + 1 << ":\n";
cout << "Name: " << database[i].name << "\n";
cout << "Phone number: " << database[i].phone << "\n";
cout << "Speaking topic: " << database[i].topic << "\n";
cout << "Fee: $" << fixed << setprecision(2) << database[i].fee << "\n";
}
}
/*******************************************************************************
* ValidateFee
* Validates that the fee is not negative.
*******************************************************************************/
void ValidateFee(float& fee) {
while (fee < 0) {
cout << "Invalid fee. Fee cannot be negative. Please enter again: ";
cin >> fee;
}
}