/* asst04x.cpp 7/6/2005 CIS 150 David G. Klick Demonstration of what would happen to the assignment #4 solution if you didn't have user-defined functions available. */ #include #include using std::cin; using std::cout; using std::fixed; using std::showpoint; using std::setprecision; int main(void) { const double PI = 3.1415926536; char choice; double area; bool valid; char *ptr; double length, width; double base, height; double side, radius; double num; char *menu = "Area Calculator Menu:\n\n" " (R)ectangle\n\n" " (C)ircle\n\n" " (H)exagon\n\n" " (T)riangle\n\n" " E(x)it\n\n"; do { // display menu cout << menu; // get user choice do { cout << "Enter choice: "; cin >> choice; ptr = strchr("RrCcTtHhXx", (int)choice); if (ptr == NULL) { cout << "Error: Invalid choice\n"; } } while (ptr == NULL); choice = toupper(choice); valid = true; // process choice switch (choice) { case 'R': do { cout << "Enter length (0-300): "; cin >> num; if (num<0.0) { cout << "Error: Input below minimum\n"; } else if (num>300.0) { cout << "Error: Input above maximum\n"; } } while (num<0.0 || num>300.0); length = num; do { cout << "Enter width (0-300): "; cin >> num; if (num<0.0) { cout << "Error: Input below minimum\n"; } else if (num>300.0) { cout << "Error: Input above maximum\n"; } } while (num<0.0 || num>300.0); width = num; area = length * width; break; case 'C': do { cout << "Enter radius (0-250): "; cin >> num; if (num<0.0) { cout << "Error: Input below minimum\n"; } else if (num>250.0) { cout << "Error: Input above maximum\n"; } } while (num<0.0 || num>250.0); radius = num; area = PI * radius * radius; break; case 'H': do { cout << "Enter side (0-200): "; cin >> num; if (num<0.0) { cout << "Error: Input below minimum\n"; } else if (num>200.0) { cout << "Error: Input above maximum\n"; } } while (num<0.0 || num>200.0); side = num; area = side * side * 2.598075; break; case 'T': do { cout << "Enter height (0-200): "; cin >> num; if (num<0.0) { cout << "Error: Input below minimum\n"; } else if (num>200.0) { cout << "Error: Input above maximum\n"; } } while (num<0.0 || num>200.0); height = num; do { cout << "Enter base (0-200): "; cin >> num; if (num<0.0) { cout << "Error: Input below minimum\n"; } else if (num>200.0) { cout << "Error: Input above maximum\n"; } } while (num<0.0 || num>200.0); base = num; area = 0.5 * height * base; break; default: valid = false; } // display area if appropriate if (valid) { cout << "The area is " << fixed << showpoint << setprecision(2) << area << "\n\n"; } // continue while user did not choose to exit } while (choice != 'X'); return 0; }