C++ Primer Plus Chapter 6 Exercise 7

c plus plusThere are a number of ways to do exercise 7. My approach uses the switch statement suggestion to keep tabs of vowels. Also, it analyses one word at a time except you have to press enter after each word for it to tally it appropriately. As mentioned, this could be completed multiple ways. Take a look at my solution below:

7.Write a program that reads input a word at a time until a lone q is entered. The program
should then report the number of words that began with vowels, the number that began
with consonants, and the number that fit neither of those categories. One approach is to
use isalpha() to discriminate between words beginning with letters and those that
don’t and then use an if or switch statement to further identify those passing the
isalpha() test that begin with vowels. A sample run might look like this:
Enter words (q to quit):
The 12 awesome oxen ambled
quietly across 15 meters of lawn. q
5 words beginning with vowels
4 words beginning with consonants
2 others

#include <iostream>
#include <string>

using namespace std;

const int size = 50;

int main()
{
char word[size];
int vowels = 0;
int consonants = 0;
int others = 0;

cout << "Enter words (q to quit): ";

while(cin.get(word, size) && (word[0] != 'q' || word[0] != 'Q'))
{
char * ch = new char;
*ch = word[0];

if(isalpha(word[0]))
{
switch(*ch)
{
case 'a' : vowels++;
break;
case 'e' : vowels++;
break;
case 'i' : vowels++;
break;
case 'o' : vowels++;
break;
case 'u' : vowels++;
break;
default  : consonants++;
break;
}
}
else
others++;
cin.get(); // keep grabbing words after enter is pressed
delete ch;
}
cout << vowels << " words beginning with vowels\n"
<< consonants << " words begining with consonants\n"
<< others << " others";

return 0;
}

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";
}

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";
}