home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C Programming Starter Kit 2.0
/
SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso
/
tybc4
/
switch2.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1993-03-19
|
974b
|
48 lines
// C++ program which uses the switch statement to implement
// a simple four-function calculator program
#include <iostream.h>
const int TRUE = 1;
const int FALSE = 0;
main()
{
double x, y, z;
char op;
int error = FALSE;
cout << "Enter the first operand: ";
cin >> x;
cout << "Enter the operator: ";
cin >> op;
cout << "Enter the second operand: ";
cin >> 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 = TRUE;
break;
default:
error = TRUE;
}
if (!error)
cout << x << " " << op << " " << y << " = " << z << "\n";
else
cout << "Bad operator or division-by-zero error\n";
return 0;
}