I have the following Speaker\'s Bureau C++ code, and I need to change the part a
ID: 2247071 • Letter: I
Question
I have the following Speaker's Bureau C++ code, and I need to change the part about gathering a Speaker's Telephone Number for each speaker into gathering a Speaker's ID Number. After that, I need to use a Try and Catch error function named InvalidSpeakerNumber if the Speaker's ID Number is outside of the range of 0 to 999. To do these two things, I need a Class implementation that shows that the exception condition works.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct speakerBureau
{
string name;
string TelephoneNumber;
string SpeakTopic;
int fee;
};
void getSpeaker(speakerBureau *);
void printSpeaker(speakerBureau *);
void editSpeaker(speakerBureau *);
void searchSpeakTopic(speakerBureau*);
int main()
{
int NUM_SPEAKERS = 10;
int index;
speakerBureau info[10];
int menu;
const int enter = 1,
change = 2,
print = 3,
search = 4,
leave = 5;
do {
cout << "Please select a choice from the menu. "
<< "1) Enter Speaker Information. "
<< "2) Change Speaker Information. "
<< "3) Print Speaker Information. "
<< "4) Search for Topic. "
<< "5) Leave this menu. "
<< "Select: ";
cin >> menu;
cout << endl;
switch (menu)
{
case enter:
{
getSpeaker(info);
}
break;
case change:
{
editSpeaker(info);
}
break;
case print:
{
printSpeaker(info);
}
break;
case search:
{
searchSpeakTopic(info);
}
}
} while (menu != leave);
system("pause");
return 0;
}
void getSpeaker(speakerBureau *p) //array name = pointer
{
int i = 0;
int size = 10;
for (i = 0; i < size; i++)
{
cout << "Please enter the following information of speaker " << i << " ";
cout << "----------------------------------------------------" << endl;
cout << "Speaker Name: ";
cin.ignore();
getline(cin, p[i].name);
cout << "Speaker Telephone Number: ";
cin.ignore();
getline(cin, p[i].TelephoneNumber);
cout << "Speaker Topic: ";
getline(cin, p[i].SpeakTopic);
cout << "Fee Required: ";
cin >> p[i].fee;
cout << endl;
}
cout << endl;
}
void printSpeaker(speakerBureau *p)
{
int i = 0;
int size = 10; // Array size
cout << "The information entered for each speaker is. ";
for (i = 0; i < size; i++)
{
cout << "Speaker " << i << endl;
cout << "-------------------------------" << endl;
cout << "Speaker Name: " << p[i].name << endl;
cout << "Speaker Telephone Number: " << p[i].TelephoneNumber << endl;
cout << "Speaker Topic: " << p[i].SpeakTopic << endl;
cout << "Speaker Fee Required: " << p[i].fee << endl << endl;
}
}
void editSpeaker(speakerBureau *p)
{
int i;
cout << "Please enter the speaker number you would like to edit: ";
cin >> i;
cout << endl;
if (i <= 9)
{
cout << endl;
cout << "Please enter the updated information of the speaker." << endl;
cout << "Speaker Name: ";
cin.ignore();
getline(cin, p[i].name);
cout << "Speaker Telephone Number: ";
cin.ignore();
getline(cin, p[i].TelephoneNumber);
cout << "Speaker Topic: ";
getline(cin, p[i].SpeakTopic);
cout << "Fee Required: ";
cin >> p[i].fee;
cout << endl;
}
else
{
cout << "Please pick a number between 0-9" << endl;
}
}
void searchSpeakTopic(speakerBureau*p)
{
int i = 0;
string topic;
cout << "Please type a topic in the program" << endl;
cin.ignore();
getline(cin, topic);
cout << endl;
int flag = 0;
for (int i = 0; i < 10; i++)
{
if (p[i].SpeakTopic == topic)
{
flag = 1;
cout << "Speaker " << i << endl;
cout << "-------------------------------" << endl;
cout << "Speaker Name: " << p[i].name << endl;
cout << "Speaker Telephone Number: " << p[i].TelephoneNumber << endl;
cout << "Speaker Topic: " << p[i].SpeakTopic << endl;
cout << "Speaker Fee Required: " << p[i].fee << endl << endl;
}
}
if (flag == 0)
cout << "No speaker found with that topic." << endl << endl;
}
Explanation / Answer
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
struct speakerBureau
{
string name;
int idNum;
string SpeakTopic;
int fee;
};
void getSpeaker(speakerBureau *);
void printSpeaker(speakerBureau *);
void editSpeaker(speakerBureau *);
void searchSpeakTopic(speakerBureau*);
int main()
{
int NUM_SPEAKERS = 10;
int index;
speakerBureau info[10];
int menu;
const int enter = 1,
change = 2,
print = 3,
search = 4,
leave = 5;
do {
cout << "Please select a choice from the menu. "
<< "1) Enter Speaker Information. "
<< "2) Change Speaker Information. "
<< "3) Print Speaker Information. "
<< "4) Search for Topic. "
<< "5) Leave this menu. "
<< "Select: ";
cin >> menu;
cout << endl;
switch (menu)
{
case enter:
{
getSpeaker(info);
}
break;
case change:
{
editSpeaker(info);
}
break;
case print:
{
printSpeaker(info);
}
break;
case search:
{
searchSpeakTopic(info);
}
}
} while (menu != leave);
system("pause");
return 0;
}
void getSpeaker(speakerBureau *p) //array name = pointer
{
int i = 0;
int size = 10;
for (i = 0; i < size; i++)
{
cout << "Please enter the following information of speaker " << i << " ";
cout << "----------------------------------------------------" << endl;
cout << "Speaker Name: ";
cin.ignore();
getline(cin, p[i].name);
cout << "Speaker IDNumber: ";
cin.ignore();
cin >> p[i].idNum;
if (p[i].idNum < 0 || p[i].idNum > 999)
throw InvalidID(p[i].idNum);
cout << "Speaker Topic: ";
getline(cin, p[i].SpeakTopic);
cout << "Fee Required: ";
cin >> p[i].fee;
cout << endl;
}
cout << endl;
}
void printSpeaker(speakerBureau *p)
{
int i = 0;
int size = 10; // Array size
cout << "The information entered for each speaker is. ";
for (i = 0; i < size; i++)
{
cout << "Speaker " << i << endl;
cout << "-------------------------------" << endl;
cout << "Speaker Name: " << p[i].name << endl;
cout << "Speaker ID Number: " << p[i].idNum << endl;
cout << "Speaker Topic: " << p[i].SpeakTopic << endl;
cout << "Speaker Fee Required: " << p[i].fee << endl << endl;
}
}
void editSpeaker(speakerBureau *p)
{
int i;
cout << "Please enter the speaker number you would like to edit: ";
cin >> i;
cout << endl;
if (i <= 9)
{
cout << endl;
cout << "Please enter the updated information of the speaker." << endl;
cout << "Speaker Name: ";
cin.ignore();
getline(cin, p[i].name);
cout << "Speaker ID Number: ";
cin.ignore();
cin >> p[i].idNum;
if (p[i].idNum < 0 || p[i].idNum > 999)
throw InvalidID(p[i].idNum);
cout << "Speaker Topic: ";
getline(cin, p[i].SpeakTopic);
cout << "Fee Required: ";
cin >> p[i].fee;
cout << endl;
}
else
{
cout << "Please pick a number between 0-9" << endl;
}
}
void searchSpeakTopic(speakerBureau*p)
{
int i = 0;
string topic;
cout << "Please type a topic in the program" << endl;
cin.ignore();
getline(cin, topic);
cout << endl;
int flag = 0;
for (int i = 0; i < 10; i++)
{
if (p[i].SpeakTopic == topic)
{
flag = 1;
cout << "Speaker " << i << endl;
cout << "-------------------------------" << endl;
cout << "Speaker Name: " << p[i].name << endl;
cout << "Speaker ID Number: " << p[i].idNum << endl;
cout << "Speaker Topic: " << p[i].SpeakTopic << endl;
cout << "Speaker Fee Required: " << p[i].fee << endl << endl;
}
}
if (flag == 0)
cout << "No speaker found with that topic." << endl << endl;
}
class InvalidID
{
private:
double value;
public:
InvalidLength(double val)
{ value = val; }
double getValue() const
{ return value; }
};