home *** CD-ROM | disk | FTP | other *** search
- /*
- C++ program that demonstrates enumerated types
- */
-
- #include <iostream.h>
-
- enum mathError { noError, badOperator, divideByZero };
-
- void sayError(mathError err)
- {
- switch (err) {
- case noError:
- cout << "No error";
- break;
- case badOperator:
- cout << "Error: invalid operator";
- break;
- case divideByZero:
- cout << "Error: attempt to divide by zero";
- }
- }
-
- main()
- {
- double x, y, z;
- char op;
- mathError error = noError;
-
- cout << "Enter a number, an operator, and a number : ";
- cin >> x >> op >> y;
-
- switch (op) {
- case '+':
- z = x + y;
- break;
- case '-':
- z = x - y;
- break;
- case '*':
- z = x * y;
- break;
- case '/':
- if (y != 0)
- z = x / y;
- else
- error = divideByZero;
- break;
- default:
- error = badOperator;
- }
-
- if (error == noError)
- cout << x << " " << op << " " << y << " = " << z;
- else
- sayError(error);
- return 0;
- }
-