/*******************************************************/ //R.A. Hillayrd //mathFunctions.cpp //Last Modified 02/26/2001 //program to demonstrate the use of the built in math functions //and a programmer defined function /*******************************************************/ #include<iostream> #include<cmath> #include<cstdlib> using namespace std; void printMenu(); //function prototypes go here int main() { char choice; bool flag = true; do { printMenu(); //print menu cin >> choice; //get choice double x,y; //variables used in switch statement switch(choice) //do user request { case 'S' : case 's' : cout << "Enter a number to calculate the square root: "; cin >> x; cout << "The square root of " << x << " is " << sqrt(x) <<endl; break; case 'P' : case 'p' : cout << "Enter x and y to calculate x to the y power: "; cin >> x >> y; cout << x << " to the " << y << " power is " << pow(x,y) <<endl; break; case 'R' : case 'r' : cout << "Random number = : " << rand() <<endl; break; case 'Q' : case 'q' : cout << "bye for now\n"; flag = false; break; default : cout << choice << " is undefined\n"; }//end switch }while(flag); //end do - while loop }//end main /******************************************************************/ //function to print menu of user choices /******************************************************************/ void printMenu() { cout << "\nWelcome to the math function program\n"; cout << "------------------------------------\n"; cout << "Choose one of the following\n"; cout << " S Calculate the square root\n"; cout << " P Calculate x to the y power\n"; cout << " R Generate a random number\n"; cout << " Q To quit\n\n"; cout << "Enter your choice: "; } /******************************************************************/ /*******************Program Output********************************* Welcome to the math function program ------------------------------------ Choose one of the following S Calculate the square root P Calculate x to the y power R Generate a random number Q To quit Enter your choice: s Enter a number to calculate the square root: 122 The square root of 122 is 11.0454 Welcome to the math function program ------------------------------------ Choose one of the following S Calculate the square root P Calculate x to the y power R Generate a random number Q To quit Enter your choice: R Random number = : 16838 Welcome to the math function program ------------------------------------ Choose one of the following S Calculate the square root P Calculate x to the y power R Generate a random number Q To quit Enter your choice: q bye for now */