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