//************************************************************************************
//
// This program keeps track of speakers' bureau.
// It displays a menu, where the user can add info,
// view the info of another speaker that's already in there,
// and etc.
//*************************************************************************************
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
//**************************************************************************************
// function prototypes
//**************************************************************************************
void displayMenu();
int getChoice(int);
void addSpeaker(Speaker&);
void viewSpeaker();
void showCount();
//**************************************************************************************
// this is the Speaker structure declaration
//**************************************************************************************
struct Speaker{
char name[128],
telNumber[128],
topic[128];
double fee;
};
//**************************************************************************************
// this function displays the menu to the user
//**************************************************************************************
void displayMenu(){
cout << "\n\tWelcome to Speaker's Bureau Menu\n\n";
cout << "1. Add a speaker\n";
cout << "2. View speaker's info\n";
cout << "3. Display the number of speakers that are on the list\n";
cout << "4. Quit the program\n\n";
return;
}
//**************************************************************************************
// this function inputs, validates and returns the user's menu choices
//**************************************************************************************
int getChoice(){
int choice;
cin >> choice;
while (choice < 1 || choice > 4){
cout << "Please, enter a valid choice number, 1-4: ";
cin >> choice;
};
return choice;
}
//**************************************************************************************
// this function lets the user add info/data to any speaker that's chosen
//**************************************************************************************
void addSpeaker(Speaker &sp){ //parameter issue &sp ???
int index = 0;
cout << "Please, choose the speaker's index that you'd like to add info to, 1-10: ";
cin >> index;
if (index == 1){ //isn't there an easier/shorter way of inputting, instead of this long if/else???
cout << "Name: ";
cin >> sp[0].name >> endl;
cout << "Telephone Number: ";
cin >> sp[0].telNumber >> endl;
cout << "Topic: ";
cin >> sp[0].topic << endl;
cout << "Required fee: ";
cin >> sp[0].fee >> endl;
};
else if (index == 2){
cout << "Name: ";
cin >> sp[1].name >> endl;
cout << "Telephone Number: ";
cin >> sp[1].telNumber >> endl;
cout << "Topic: ";
cin >> sp[1].topic << endl;
cout << "Required fee: ";
cin >> sp[1].fee >> endl;
};
else if (index == 3){
cout << "Name: ";
cin >> sp[2].name >> endl;
cout << "Telephone Number: ";
cin >> sp[2].telNumber >> endl;
cout << "Topic: ";
cin >> sp[2].topic << endl;
cout << "Required fee: ";
cin >> sp[2].fee >> endl;
};
else if (index == 4){
cout << "Name: ";
cin >> sp[3].name >> endl;
cout << "Telephone Number: ";
cin >> sp[3].telNumber >> endl;
cout << "Topic: ";
cin >> sp[3].topic << endl;
cout << "Required fee: ";
cin >> sp[3].fee >> endl;
};
else if (index == 5){
cout << "Name: ";
cin >> sp[4].name >> endl;
cout << "Telephone Number: ";
cin >> sp[4].telNumber >> endl;
cout << "Topic: ";
cin >> sp[4].topic << endl;
cout << "Required fee: ";
cin >> sp[4].fee >> endl;
};
else if (index == 6){
cout << "Name: ";
cin >> sp[5].name >> endl;
cout << "Telephone Number: ";
cin >> sp[5].telNumber >> endl;
cout << "Topic: ";
cin >> sp[5].topic << endl;
cout << "Required fee: ";
cin >> sp[5].fee >> endl;
};
else if (index == 7){
cout << "Name: ";
cin >> sp[6].name >> endl;
cout << "Telephone Number: ";
cin >> sp[6].telNumber >> endl;
cout << "Topic: ";
cin >> sp[6].topic << endl;
cout << "Required fee: ";
cin >> sp[6].fee >> endl;
};
else if (index == 8){
cout << "Name: ";
cin >> sp[7].name >> endl;
cout << "Telephone Number: ";
cin >> sp[7].telNumber >> endl;
cout << "Topic: ";
cin >> sp[7].topic << endl;
cout << "Required fee: ";
cin >> sp[7].fee >> endl;
};
else if (index == 9){
cout << "Name: ";
cin >> sp[8].name >> endl;
cout << "Telephone Number: ";
cin >> sp[8].telNumber >> endl;
cout << "Topic: ";
cin >> sp[8].topic << endl;
cout << "Required fee: ";
cin >> sp[8].fee >> endl;
};
else if (index == 10){
cout << "Name: ";
cin >> sp[9].name >> endl;
cout << "Telephone Number: ";
cin >> sp[9].telNumber >> endl;
cout << "Topic: ";
cin >> sp[9].topic << endl;
cout << "Required fee: ";
cin >> sp[9].fee >> endl;
};
else {
cout << "Please, enter a valid index number: ";
};
return;
}
//**************************************************************************************
// this function lets the user see the info of a chosen speaker
//**************************************************************************************
void viewSpeaker(Speaker sp){
cout << "Please, choose the speaker's index, 1-10, "
<< "that you are interested in, and it will be displayed: ";
cin >> index;
if (index == 1){
cout << sp[0].name << " - " << sp[0].telNumber << " - " << sp[0].topic << emdl;
};
else if (index == 2){
cout << sp[1].name << " - " << sp[1].telNumber << " - " << sp[1].topic << endl;
};
else if (index == 3){
cout << sp[2].name << " - " << sp[2].telNumber << " - " << sp[2].topic << endl;
};
else if (index == 4){
cout << sp[3].name << " - " << sp[3].telNumber << " - " << sp[3].topic << endl;
};
else if (index == 5){
cout << sp[4].name << " - " << sp[4].telNumber << " - " << sp[4].topic << endl;
};
else if (index == 6){
cout << sp[5].name << " - " << sp[5].telNumber << " - " << sp[5].topic << endl;
};
else if (index == 7){
cout << sp[6].name << " - " << sp[6].telNumber << " - " << sp[6].topic << endl;
};
else if (index == 8){
cout << sp[7].name << " - " << sp[7].telNumber << " - " << sp[7].topic << endl;
};
else if (index == 9){
cout << sp[8].name << " - " << sp[8].telNumber << " - " << sp[8].topic << endl;
};
else if (index == 10){
cout << sp[9].name << " - " << sp[9].telNumber << " - " << sp[9].topic << endl;
};
else {
cout << "Please, enter a valid index number: ";
};
return;
}
//**************************************************************************************
// main function
//**************************************************************************************
int main(){
struct Speaker sp[10] = {{"_", "_", "_", 0},
{"_", "_", "_", 0},
{"_", "_", "_", 0},
{"_", "_", "_", 0},
{"_", "_", "_", 0},
{"_", "_", "_", 0},
{"_", "_", "_", 0},
{"_", "_", "_", 0},
{"_", "_", "_", 0},
{"_", "_", "_", 0}}; //info spaces for 10 speakers that are ready to be filled
do{
int choice;
displayMenu();
choice = getChoice();
if(choice == 1){
addSpeaker();
};
else if(choice == 2){
viewSpeaker();
};
else if(choice == 3){
showCount()
};
else if(choice == 4){
cout << "Thanks for using my app. Bye.";
exit(0);
};
}while (choice != 0);
return 0;
}
////////////////////////////////////////////////////////////////////////////////////
Hey, guys
i'm trying to write a program that basically, lets the user choose from a menu option. The menu is all the way up top. I was going fine till I hit the addSpeaker function. As you can see, i wrote down bunch of if/else and cout/cin statements. I was wondering, isn't there some other shorter way of doing it? like, when you wanna assign some number to something, you can use "for" loop and the program goes through everything and finds that spot and puts that number, and etc. You know what i mean? like, instead of writing bunch of lines of code, isn't there something shorter and simpler?
anybody?
thank you