I am learning C++ from the Stroustrup book "Programming Principles and Practice". I have been doing the exercises in each chapter. I have gotten stuck on chapter 6. I understand the concepts of classes, constructors, functions, and 'throw and catch' error handling, but seemingly not well enough to actually write a program with these elements that compiles and runs. I have written a program for exercise 10 in chapter 6 that runs almost correctly.
It is not a pretty program and the error exception for non-integer inputs causes the program to crash with a message relating to error exception #3 instead of my intended #4. I don't know how to put all the error exceptions into the same class. Thank you in advance for your help!
N.B. You will have to change the header line if you want to run this program. The header in the program is the one Stroustrup instructs us to download from his site
#include "../../std_lib_facilities.h"
class myexception1: public exception // Classes for handling errors.
{
virtual const char* what() const throw()
{
return "The set and subset sizes must be greater than 0.\n";
}
} myex1;
class myexception2: public exception
{
virtual const char* what() const throw()
{
return "The set size must be greater than the subset size.\n";
}
} myex2;
class myexception3: public exception
{
virtual const char* what() const throw()
{
return "Either a 'c' for combination or a 'p' for permutation must be indicated.\n";
}
} myex3;
class myexception4: public exception
{
virtual const char* what() const throw()
{
return "The set and subset sizes must be integers.\n";
}
} myex4;
int factorial (int x) // Factorial function using the recursive method.
{
int f;
if (x == 0) {
f = 1;
} else {
f = x * factorial(x-1);
}
return (f);
}
int permu (int n, int r) // Ordered arrangement calculation (permuatation) for subset of size 'r' out of a set of size 'n'.
{
int pm;
pm = factorial(n) / factorial(n-r);
return pm;
}
int combi (int n, int r) // Unordered arrangement calculation (combination) for subset of size 'r' out of a set of size 'n'.
{
int cb;
cb = permu(n,r) / factorial(r);
return cb;
}
int main ()
try {
int c = 0;
int d = 0;
char z = ' ';
int result = 0;
cout <<"Enter the set size which must be greater than zero, the subset size, \nand the type of computation.\n";
cout <<"The set size must be greater the subset size. \nEnter 'c' for combination or 'p' for permutation.\n";
cout <<"Separate your entries by a space.\n";
cin >> c >> d >> z;
if (c <= 0 || d < 0) throw myex1; // Negative size error.
if (c < d) throw myex2; // Set size less than subet size error.
if (z != 'c' && z != 'p') throw myex3; // Choice other than combination or permutation error.
if (c%1 != 0 || d%1 != 0) throw myex4; // Non-integer error.
if (z == 'p') {
result = permu(c,d);
cout << "The number of permutations is " << result <<".\n";
}
if (z == 'c') {
result = combi(c,d);
cout << "The number of combinations is " << result <<".\n";
}
keep_window_open();
return 0;
} catch(exception& e) {
cerr << e.what() << endl;
keep_window_open();
return 1;
} catch(...) {
cerr <<"Something else went wrong.\n";
keep_window_open();
return 2;
}
.