/* lab04.cpp CIS 150 6/2/2005 David Klick This program demonstrates the use of selection structures in C++. */ #include #include using std::cin; using std::cout; using std::setprecision; using std::fixed; using std::showpoint; int main(void) { const double PI = 3.1415926536; char choice; // user's menu choice bool valid = true; // whether user input was valid bool quit = false; // whether user chose to quit program double radius; // for circle double length, width; // for rectangle double base, height; // for triangle double area; // for results of all calcs // display menu cout << "Area calculator\n\n" << "1. (C)ircle\n\n" << "2. (R)ectangle\n\n" << "3. (T)riangle\n\n" << "4. E(x)it\n\n" << "Choice: "; cin >> choice; // menu choice processing switch (choice) { case '1': case 'C': case 'c': cout << "\nEnter radius of circle ( >0 ): "; cin >> radius; if (radius <= 0.0) { cout << "Error: radius out of range\n"; valid = false; } else { area = PI * radius * radius; } break; case '2': case 'R': case 'r': cout << "\nEnter length and width of rectangle ( >0 ): "; cin >> length >> width; if (length <= 0.0) { cout << "Error: length out of range\n"; valid = false; } if (width <= 0.0) { cout << "Error: width out of range\n"; valid = false; } if (valid) { area = length * width; } break; case '3': case 'T': case 't': cout << "\nEnter base and height of triangle ( >0 ): "; cin >> base >> height; if (base <= 0.0) { cout << "Error: base out of range\n"; valid = false; } if (height <= 0.0) { cout << "Error: height out of range\n"; valid = false; } if (valid) { area = 0.5 * base * height; } break; case '4': case 'X': case 'x': cout << "Thank you for using AreaCalc XP\n"; quit = true; break; default: cout << "Error: invalid menu choice\n"; valid = false; } // display area if no errors encountered and // user didn't choose to quit if (!quit && valid) { cout << "The area is " << fixed << showpoint << setprecision(3) << area << '\n'; } cout << "Normal program termination\n"; return 0; }