In: Computer Science
Starting out with Control Structures: Program challenges in C++ by Tony Gaddis
This problem was adapted from questions 9 and 10 on page 661
Write a program that keeps track of a speakers’ bureau. The program should use a structure to store the following data about a speaker:
Name, Telephone Number, Speaking Topic, Fee Required. The program should use a vector of structures. It should let the user enter data into the vector, change the contents of any element, and display all the data stored in the vector. The program should have a menu-driven user interface. Input Validation: When the data for a new speaker is entered, be sure the user enters data for all the fields. No negative amounts should be entered for a speaker’s fee.
In addition: add a function to the program that allows the user to search for a speaker on a particular topic. It should accept a key word as an argument then search the vector for a structure with that key word in the Speaking Topic field. All structures that match should be displayed. If no structure matches, a message saying so should be displayed. Hint: use the .find() string function to search the Speaking Topic field.
#include <iostream>
#include<vector>
using namespace std;
struct Speaker{
string name;
string phone;
string topic;
double fee;
//constructor
Speaker(string n, string p , string t, double
f){
name = n;
phone = p;
topic = t;
fee = f;
}
};
void print(Speaker s){
cout << "Name : " << s.name <<
endl;
cout << "Telephone Number : " <<
s.phone << endl;
cout << "Speaking Topic : " <<
s.topic << endl;
cout << "Speaking fee : " << s.fee
<< endl;
cout << endl;
}
int main() {
vector<Speaker> v;
while(true){
char ch;
cout << "Press c to continue
or any other key to exit" << endl;
cin >> ch;
if(ch != 'c'){
return 0;
}
cout << "Enter 1 to add a
speaker or 2 to search the list of all speakers" <<
endl;
int choice;
cin >> choice;
while(choice != 1 &&
choice != 2){
cout <<
"Please enter the correct choice" << endl;
cout <<
"Enter 1 to add speaker or 2 to view the list of all speakers"
<< endl;
cin >>
choice;
}
if(choice == 1){
cout <<
"Enter name of speaker" << endl;
string name =
"";
cin >>
name;
while(name ==
""){
cout << "please enter a valid name"
<< endl;
cin >> name;
}
cout <<
"Enter telephone number of speaker" << endl;
string phone =
"";
cin >>
phone;
while(phone ==
""){
cout << "please enter a valid telephone
number" << endl;
cin >> phone;
}
cout <<
"Enter the speaking topic of speaker" << endl;
string topic =
"";
cin >>
topic;
while(topic ==
""){
cout << "please enter a valid topic"
<< endl;
cin >> topic;
}
cout <<
"Enter speaking fee of speaker" << endl;
double
fee;
cin >>
fee;
while(fee <
0){
cout << "please enter a non negative fee"
<< endl;
cin >> fee;
}
Speaker
newEntry(name,phone,topic,fee);
v.push_back(newEntry);
}
else{
cout << "Enter topic to search" << endl;
string query;
cin >> query;
cout << "Speakers that match are : " << endl;
bool flag = false; // to check if any matches or not
for(int i = 0 ; i < v.size() ; i++){
Speaker s = v[i];
if(s.topic == query){
flag = true;
print(s);
}
}
if(!flag)
cout << "None" << endl;
}
}
return 0;
}
Out