// // Program name: modified_quiz05_pgm // Author: B. Massingill // // Purpose: compute perimeter of a triangle with vertices specified // as points in 2D space // // Input: // three pairs of (x, y) coordinates representing three points in // 2D space // Output: // perimeter of the triangle formed by the three points // #include // **** function prototypes and comments **** // function to obtain two integers from standard input, prompting user // precondition: // msg = string to use in prompting user // postcondition: // x, y are integers read from standard input // notes: // user is prompted with message "Please enter " plus msg. // if what is entered is not two integers, program displays // error message and tries again, continuing until it obtains // two integers. void obtain2Integers(const char msg[], int& x, int& y); // function to compute 2D distance // precondition: // (x1, y1) and (x2, y2) are coordinates of two points in // 2D space // postcondition: // return value is distance between (x1, y1) and (x2, y2) double twoDdistance(int x1, int y1, int x2, int y2); // helper function for obtain2Integers // precondition: // none // postcondition: // return value is an integer read from standard input // notes: // if next thing read is not an integer, program displays error // message and tries again, continuing until it reads an integer. int obtainInteger(void); // **** main program **** int main() { int x1, y1, x2, y2, x3, y3; obtain2Integers("x, y coordinates for point 1", x1, y1); obtain2Integers("x, y coordinates for point 2", x2, y2); obtain2Integers("x, y coordinates for point 3", x3, y3); cout << "perimeter is " << twoDdistance(x1, y1, x2, y2) + twoDdistance(x2, y2, x3, y3) + twoDdistance(x3, y3, x1, y1) << endl; return 0 ; } // **** function definitions **** void obtain2Integers(const char msg[], int& x, int& y) { cout << "Please enter " << msg << ": "; x = obtainInteger(); y = obtainInteger(); } // modified version of obtainInteger from ASCII art program int obtainInteger(void) { int c; char ignoredChar; bool goodInput = false; do { if (cin >> c) { goodInput = true; } else { cerr << "Please enter an integer!\n"; if (cin.eof()) // received Control-D! cin.clear(); else { // received a non-integer cin.clear(); cin.get(ignoredChar); } } } while (!goodInput); return c; } double twoDdistance(int x1, int y1, int x2, int y2) { return sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)); }