In exercise 4 we step up our menu skills a notch or two. This time, we are using a struct to declare some members and we want to dynamically fill them using an array. Otherwise, this is very similar to exercise 3 in that we are still using switch case. There are some syntactical nuances you must navigate in order to get your output to display correctly and not enter a forever loop. Check out my source below:
4. When you join the Benevolent Order of Programmers, you can be known at BOP meetings
by your real name, your job title, or your secret BOP name. Write a program that
can list members by real name, by job title, by secret name, or by a member’s preference.
Base the program on the following structure:
// Benevolent Order of Programmers name structure
struct bop {
char fullname[strsize]; // real name
char title[strsize]; // job title
char bopname[strsize]; // secret BOP name
int preference; // 0 = fullname, 1 = title, 2 = bopname
};
In the program, create a small array of such structures and initialize it to suitable values.
Have the program run a loop that lets the user select from different alternatives:
a. display by name b. display by title
c. display by bopname d. display by preference
q. quit
Note that “display by preference” does not mean display the preference member; it
means display the member corresponding to the preference number. For instance, if
preference is 1, choice d would display the programmer’s job title. A sample run may
look something like the following:
Benevolent Order of Programmers Report
a. display by name b. display by title
c. display by bopname d. display by preference
q. quit
Enter your choice: a
Wimp Macho
Raki Rhodes
Celia Laiter
Hoppy Hipman
Pat Hand
Next choice: d
Wimp Macho
Junior Programmer
MIPS
Analyst Trainee
LOOPY
Next choice: q
Bye!
#include <iostream> #include <string> #include <cctype> using namespace std; // Benevolent Order of Programmers name structure struct bop { char fullname[40]; // real name char title[40]; // job title char bopname[40]; // secret BOP name int preference; // 0 = fullname, 1 = title, 2 = bopname }; void showMenu(); int main() { // Initialize struct array bop programmer[5]={ "Cameron Anglin", "Senior Developer", "rundata", 1, "Bill Gates", "Quality Assurance", "Bill77", 2, "Larry Wall", "Debugger", "LW", 0, "Fusajiro Yamauchi", "Game Tester", "Yoshi", 1, "Bjarne Stroustrup", "The Creator", "C++", 0 }; showMenu(); char choice; do { cin >> choice; choice = tolower(choice); if (choice != 'a' && choice != 'b' && choice !='c' && choice !='d') continue; else for(int i = 0; i < 5; i++) { switch(choice) { case 'a': cout << programmer[i].fullname << "\n"; break; case 'b': cout << programmer[i].title << "\n"; break; case 'c': cout << programmer[i].bopname << "\n"; break; case 'd': switch(programmer[i].preference) { case 0: cout << programmer[i].fullname << "\n"; break; case 1: cout << programmer[i].title << "\n"; break; case 2: cout << programmer[i].bopname << "\n"; break; } } } cout << "\n"; showMenu(); }while(choice != 'Q' && choice != 'q'); cout << "Bye!\n"; return 0; } void showMenu() { cout << "Benevolent Order of Programmers Report\n"; cout << "a. display by name b. display by title\n"; cout << "c. display by bopname d. display by preference\n"; cout << "q. quit\n\n"; cout << "Enter your choice: \n"; }