C++ Primer Plus Chapter 6 Exercise 4

c plus plusIn 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";
}
Advertisements

C++ Primer Plus Chapter 6 Exercise 3

c plus plusExercise 3 is interesting because it shows us how a rudimentary menu can be made using simple switch statements. Check out the source for my solution:

3. Write a precursor to a menu-driven program. The program should display a menu offering
four choices, each labeled with a letter. If the user responds with a letter other than
one of the four valid choices, the program should prompt the user to enter a valid
response until the user complies. Then the program should use a switch to select a simple
action based on the user’s selection. A program run could look something like this:
Please enter one of the following choices:
c) carnivore p) pianist
t) tree g) game
f
Please enter a c, p, t, or g: q
Please enter a c, p, t, or g: t
A maple is a tree.

#include <iostream>

using namespace std;

void showMenu();
void carnivore();
void tree();
void pianist();
void game();

int main()
{
char choice;

showMenu();
cin >> choice;
while(choice != 'Q' && choice != 'q')
{
switch(choice)
{
case 'C':
case 'c': carnivore();
break;
case 'P':
case 'p': pianist();
break;
case 'T':
case 't': tree();
break;
case 'G':
case 'g': game();
break;
default: cout << "That is not a choice!\n";
}
showMenu();
cin >> choice;
}
return 0;
}

void showMenu()
{
cout << "Please enter one of the following choices: \n\n";
cout << "c) carnivore p) pianist\n";
cout << "t) tree      g) game\n";
cout << "q) quit\n";
cout << "\n";
}

void carnivore()
{
cout << "Rawr, you have selected carnivore!\n";
}

void tree()
{
cout << "Behold, a tree has spawned!\n";
}

void pianist()
{
cout << "Is this Beethoven?\n";
}

void game()
{
cout << "Shall we play a game?\n";
}