/************************************************************************* * Filename: choices.cpp * * Purpose: This program presents the user with an interactive menu * of options to perform some common computations. For a list * of these computations, see the code of function displayMenu. * * Programmer: Jason M. Snouffer & Matthew Giampiccolo * (Thanks to Andrea Lobo with modifications by Jennifer Kay) * * Completed: Wednesday, October 23, 2002 * * Course: Computer Science and Programming * * * Modification log: * * 10/23/2002: Modified by Jason M. Snouffer & Matthew Giampiccolo * -Added the perfect number function * * 10/23/2002: Modified by Jason M. Snouffer & Matthew Giampiccolo * -All known bugs have been fixed. * * 10/23/2002 Modified by Jennifer Kay. The same bugs still exist! * * 9/22/2002 Created by Andrea Lobo * Bugs at this time: * Menu options 2-5 have not been implemented. * The existing functions have not been tested. * The program does not compile. * * Known bugs: * 9/22/2002 This program is missing some function prototypes and calls. * The existing functions have not been tested. * The program does not compile. ****************************************************************************/ /* Test Suite: Note: After the last expected output for each of the scenarios listed below, the program will output "Next Choice? (6 to see menu):" and the program will repeat. Initial Expected Output: Here are your choices: 1 to compute the maximum of two integers. 2 to compute the average of three integers. 3 to find out whether an integer is odd or even. 4 to display a face picture. 5 to display all integers between two input values. 6 to display the menu again. 0 to quit. Initial Input (menu item): 0 Expected Output: Thanks for using this program! Initial Input (menu item): -5 Expected Output: Invalid value. Please re-enter a value between 0 and 6: Initial Input (menu item): 10 Expected Output: Invalid value. Please re-enter a value between 0 and 6: Initial Input (menu item): 1 Expected Output: Enter the two integers that you want to know the max of: Input: 2, 5 Expected Output: The maximum of 2 and 5 is 5. Initial Input (menu item): 1 Expected Output: Enter the two integers that you want to know the max of: Input: 4, -1 Expected Output: The maximum of 4 and -1 is 4. Initial Input (menu item): 1 Expected Output: Enter the two integers that you want to know the max of: Input: 10, 10 Expected Output: The maximum of 10 and 10 is 10. Initial Input (menu item): 1 Expected Output: Enter the two integers that you want to know the max of: Input: -20, -30 Expected Output: The maximum of -20 and -30 is -20. Initial Input (menu item): 1 Expected Output: Enter the two integers that you want to know the max of: Input: 25.5, 25 Expected Output: The maximum of 25.5 and 25 is 25. (actual output: ) Initial Input (menu item): 2 Expected Output: Enter the three integers you would like to average: Input: 3, 6, 7 Expected Output: The average of 3, 6, and 7 is 5.33333. Initial Input (menu item): 2 Expected Output: Enter the three integers you would like to average: Input: 10, 100, 53 Expected Output: The average of 10, 100, and 53 is 54.3333. Initial Input (menu item): 2 Expected Output: Enter the three integers you would like to average: Input: -5, 7, 476 Expected Output: The average of -5, 7, and 476 is 149.333. Initial Input (menu item): 2 Expected Output: Enter the three integers you would like to average: Input: 10.5, 32 ,65 Expected Output: Initial Input (menu item): 2 Expected Output: Enter the three integers you would like to average: Input: -30, -8367, 1000034 Expected Output: The average of -30, -8367, and 1000034 is 5.33333. Initial Input (menu item): 2 Expected Output: Enter the three integers you would like to average: Input: 23, 23, 23 Expected Output: The average of 23, 23, and 23 is 23. Initial Input (menu item): 2 Expected Output: Enter the three integers you would like to average: Input: 5885, 10, 1 Expected Output: The average of 5885, 10, and 1 is 1965.33. Initial Input (menu item): 3 Expected Output: Enter an integer: Input: 5 Expected Output: The integer 5 is odd. Initial Input (menu item): 3 Expected Output: Enter an integer: Input: -11 Expected Output: The integer -11 is odd. Initial Input (menu item): 3 Expected Output: Enter an integer: Input: 0 Expected Output: The integer 0 is even. Initial Input (menu item): 3 Expected Output: Enter an integer: Input: 27892 Expected Output: The integer 27892 is even. Initial Input (menu item): 3 Expected Output: Enter an integer: Input: -4 Expected Output: The integer -4 is even. Initial Input (menu item): 3 Expected Output: Enter an integer: Input: 22223 Expected Output: The integer 22223 is odd. Initial Input (menu item): 4 Expected Output: ***** * * * + + * * - * * !_/ * * * *** Initial Input (menu item): 5 Expected Output: Enter the lower and upper bounds: Input: 5, 11 Expected Output: 5 6 7 8 9 10 11 Initial Input (menu item): 5 Expected Output: Enter the lower and upper bounds: Input: 99, 103 Expected Output: 99 100 101 102 103 Initial Input (menu item): 5 Expected Output: Enter the lower and upper bounds: Input: 1, 1000 Expected Output: Initial Input (menu item): 5 Expected Output: Enter the lower and upper bounds: Input: 1, 900000 Expected Output: Initial Input (menu item): 5 Expected Output: Enter the lower and upper bounds: Input: -100, 0 Expected Output: Initial Input (menu item): 5 Expected Output: Enter the lower and upper bounds: Input: 10, 5 Expected Output: Initial Input (menu item): 5 Expected Output: Enter the lower and upper bounds: Input: 35, 35 Expected Output: 35 Initial Input (menu item): 6 Expected Output: Here are your choices: 1 to compute the maximum of two integers. 2 to compute the average of three integers. 3 to find out whether an integer is odd or even. 4 to display a face picture. 5 to display all integers between two input values. 6 to display the menu again. 7 to determine if an integer is perfect. 0 to quit. Initial Input (menu item): 7 Expected Output: Enter an integer Input: 6 Expected Output: 6 is perfect. Initial Input (menu item): 7 Expected Output: Enter an integer Input: 28 Expected Output: 28 is perfect. Initial Input (menu item): 7 Expected Output: Enter an integer Input: 496 Expected Output: 496 is perfect. Initial Input (menu item): 7 Expected Output: Enter an integer Input: 8 Expected Output: 8 is not perfect. Initial Input (menu item): 7 Expected Output: Enter an integer Input: 1 Expected Output: 1 is not perfect. Initial Input (menu item): 7 Expected Output: Enter an integer Input: 0 Expected Output: 0 is not perfect. Initial Input (menu item): 7 Expected Output: Enter an integer Input: 120 Expected Output: 120 is not perfect. Initial Input (menu item): 7 Expected Output: Enter an integer Input: -300 Expected Output: -300 is not perfect. All of the sample inputs within this test suite were tested and performed as expected. */ #include #include using namespace std; // *** function prototypes go here *** // display the menu of choices to the user // // Preconditions: none // Postconditions: A menu is displayed for the user on the screen void displayMenu (); // read (repeatedly if necessary) an integer value from the user // between lowerBound and upperBound, inclusive. Return the // first valid value the user inputs. (This is similar to // the get_pos_int we wrote in class, except that it // gets an int between two bounds instead of one that is // greater than zero // // Pre: none // Post: As above, the number returned is between // lowerBound and upperBound inclusive int readBoundedInt (int lowerBound, int upperBound); // returns the maximum of its arguments // pre: none // post: the value returned is the maximum of num1 and num2 int max (int num1,int num2); // returns the average of three integers // pre: none // post: returns the average of the 3 integers double calcAverage3Ints (int num1, int num2, int num3); // return true if num is even; false if it is odd // *** CSP class notice the bool type!!! // pre: none // post: returns true if num is even, false if it's odd bool isEven (int num); // display a face using characters from the keyboard // pre: none // post: a face will be displayed on the screen void displayFace (); // display a face using characters from the keyboard // pre: none // post: a face will be displayed on the screen void displayValuesBetween (int lowerBound, int upperBound); // determines if a number is perfect or not // // Preconditions: none // Postconditions: A boolean is returned stating if number is perfect or not bool isPerfect (int number); int main() { int choice; // input: user selection of what computation to execute next int data1, // input: user data to be used in computation data2, data3; // Run the display menu function and read the first choice from user displayMenu(); cout << "What is your choice? "; choice = readBoundedInt (0, 7); // valid choices are 0-7, inclusive // repeatedly execute various computations for the user, // given the user's selection from a menu of options. while (choice) // we could have also written: while (choice !=0) { // execute one computation for the user switch (choice) { case 1: // read two integers from the user and displays their maximum cout << "Enter the two integers that you want to know the max of: "; cin >> data1 >> data2; cout << "The maximum of " << data1 << " and " << data2 << " is " << max (data1, data2) << "." << endl; break; case 2: // read three integers from the user and displays their average cout << "Enter the three integers you would like to average: "; cin >> data1 >> data2 >> data3; cout << "The average of " << data1 << ", " << data2 << ", and " << data3 << " is " << calcAverage3Ints (data1, data2, data3) << "." << endl; break; case 3: // reads an integer from a user and determines whether it is even or odd cout << "Enter an integer: "; cin >> data1; if ( isEven(data1) ) { cout<< "The integer " << data1 <<" is even.\n"; } else { cout<< "The integer " << data1 <<" is odd.\n"; } break; case 4: //calls upon function displayFace() to display a small text-based graphic displayFace (); break; case 5: //reads in 2 integers and displays all integers between those numbers cout<<"Enter the lower and upper bounds: "; cin >> data1 >> data2; displayValuesBetween (data1,data2); break; case 6: displayMenu (); break; case 7: //reads in an integer and determines if it is perfect cout<<"Enter an integer: "; cin>>data1; if ( isPerfect(data1) ) { cout<> readInt; // display error message in case value of readInt is invalid if ( ! ( (lowerBound <= readInt) && (readInt <= upperBound) ) ) cout << "Invalid value. Please re-enter a value between " << lowerBound << " and " << upperBound << ": "; } while ( ! ( (lowerBound <= readInt) && (readInt <= upperBound) ) ); return readInt; } // readBoundedInt // -------------------- max --------------------------- // returns the maximum of its arguments // pre: none // post: the value returned is the maximum of num1 and num2 int max (int num1, //inputs int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; } // max // -------------------- calcAverage3Ints --------------------------- // returns the average of three integers // pre: none // post: returns the average of the 3 integers double calcAverage3Ints (int num1, int num2, int num3) { double ave; int sum; sum = num1 + num2 + num3; ave = sum / 3.0; return ave; } // calcAverage3Ints // -------------------- isEven --------------------------- // return true if num is even; false if it is odd // *** CSP class notice the bool type!!! // pre: none // post: returns true if num is even, false if it's odd bool isEven (int num) // input { bool result; if ( (num % 2) == 0) result = true; else result = false; return result; } // isEven // -------------------- displayFace --------------------------- // display a face using characters from the keyboard // pre: none // post: a face will be displayed on the screen void displayFace () { cout << "\n"; cout << " *****\n"; cout << " * *\n"; cout << " * + + *\n"; cout << " * - *\n"; cout << " * !_/ *\n"; cout << " * *\n"; cout << " ***\n"; cout << "\n"; } // displayFace // -------------------- displayValuesBetween --------------------------- // displays the integer values between lowerBound and upperBound. // PRE: lowerBound <= upperBound // post: the values between lowerBound and upperBound will be displayed void displayValuesBetween (int lowerBound, // inputs int upperBound) { int i; for (i = lowerBound; i <= upperBound; i++) cout << i << endl; } // displayValuesBetween bool isPerfect (int number) { int i, sum(0); for(i = 1; i < number; i++) { if ( number%i == 0 ) sum += i; } if (number == sum) return true; else return false; // return (number == sum) }