home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / tybc4 / enum1.cpp < prev    next >
C/C++ Source or Header  |  1993-03-26  |  1KB  |  58 lines

  1. /*
  2.   C++ program that demonstrates enumerated types
  3. */
  4.  
  5. #include <iostream.h> 
  6.  
  7. enum mathError { noError, badOperator, divideByZero };
  8.  
  9. void sayError(mathError err)
  10. {
  11.   switch (err) {
  12.     case noError:
  13.       cout << "No error";
  14.       break;
  15.     case badOperator:
  16.       cout << "Error: invalid operator";
  17.       break;
  18.     case divideByZero:
  19.       cout << "Error: attempt to divide by zero";
  20.   }
  21. }
  22.  
  23. main()
  24. {
  25.   double x, y, z;
  26.   char op;
  27.   mathError error = noError;
  28.   
  29.   cout << "Enter a number, an operator, and a number : ";
  30.   cin >> x >> op >> y;       
  31.   
  32.   switch (op) {
  33.     case '+':
  34.       z = x + y;
  35.       break;
  36.     case '-':
  37.       z = x - y;
  38.       break;
  39.     case '*':
  40.       z = x * y;
  41.       break;
  42.     case '/':
  43.       if (y != 0)
  44.         z = x / y;
  45.       else
  46.         error = divideByZero;
  47.       break;                     
  48.     default:
  49.       error = badOperator;
  50.   }
  51.                               
  52.   if (error == noError)
  53.     cout << x << " " << op << " " << y << " = " << z;
  54.   else
  55.     sayError(error);                              
  56.   return 0;
  57. }
  58.