# C++ Primer Plus Chapter 6 Exercise 5

Exercise 5 requires we make a looping program to calculate tax, except the tax part is not really conventional. For instance, it is additive throughout different dollar amount. This make our else if structs a little more complicated.  A do-while loop satisfies the requirements of this program nicely, see my source below:

5. The Kingdom of Neutronia, where the unit of currency is the tvarp, has the following
income tax code:
first 5,000 tvarps: 0% tax
next 10,000 tvarps: 10% tax
next 20,000 tvarps: 15% tax
tvarps after 35,000: 20% tax
For example, someone earning 38,000 tvarps would owe 5,000 × 0.00 + 10,000 × 0.10
+ 20,000 × 0.15 + 3,000 × 0.20, or 4,600 tvarps. Write a program that uses a loop to
solicit incomes and to report tax owed. The loop should terminate when the user enters
a negative number or nonnumeric input.

```#include <iostream>

using namespace std;

int main()
{
double tvarp = 0.0;
double tax;

do
{
cout << "Enter Tvarps earned this year: \n";
if(tvarp < 0 || !(cin >> tvarp))
{
cout << "Invalid input, non-negative numbers only!\n";
cin.clear();
}
else if (tvarp > 5000 && tvarp <= 15000)
tax = 5000*0 + (tvarp - 5000)*0.10;
else if (tvarp > 15000 && tvarp <= 35000)
tax = 5000*0 + 10000*0.10 + (tvarp - 15000)*0.15;
else if (tvarp > 35000)
tax = 5000*0 + 10000*0.10 + 20000*0.15 + (tvarp - 35000)*0.20;
else tax = 0;

cout << "Your tax is: " << tax << endl;
}
while(tvarp <= 0);

return 0;
}
```

# C++ Primer Plus Chapter 6 Exercise 4

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

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

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";
}while(choice != 'Q' && choice != 'q');

cout << "Bye!\n";
return 0;
}

{
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

Exercise 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 carnivore();
void tree();
void pianist();
void game();

int main()
{
char choice;

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";
}
cin >> choice;
}
return 0;
}

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

# C++ Primer Plus Chapter 6 Exercise 2

The trick to exercise 2 is not just getting it to average numbers, but getting it to produce how many where over the average. See my solution to this below:

2. Write a program that reads up to 10 donation values into an array of double. The program
should terminate input on non-numeric input. It should report the average of the
numbers and also report how many numbers in the array are larger than the average.

```#include <iostream>

const int max = 10;

int main()
{
double donations[max];

std::cout << "Enter your donation amounts\n\n";
std::cout << "Donation #1: ";

int i = 0;
while(i < max && std::cin >> donations[i])
{
if(++i < max)
std::cout << "Donation #" << i+1 << ": ";
}

double total = 0.0;
int over = 0;
for(int j = 0; j < i; j++)
{
total+=donations[j];
}

int avg = total / i;

for(int j = 0; j < i; j++)
{
if(donations[j] > avg)
over++;
}

std::cout << "\n";
std::cout <<  avg << " Average of donations\n";
std::cout << "Number of donations over the average amount " << over << "\n";

return 0;
}
```