/* testsel5.cpp CIS 150 6/5/2008 David Klick Demonstration of conditional (?:) operator. Calculates the maximum value three different ways. */ #include using std::cout; using std::cin; void cont(); int main(void) { // Note: Although style rules often demand that each variable // be declared on a separate line, C++ actually allows multiple // declarations per line. int x, y; int max1, max2, max3; // Note: Multiple assignments can be done at the same time. // The assignments are done right-to-left x = y = max1 = max2 = max3 = 0; // get two integers from user cout << "Enter two integers: "; cin >> x >> y; // first way // - use simple if/else to get max value if (x > y) { max1 = x; } else { max1 = y; } // second way // - assume x is max value, then check to see if y is bigger max2 = x; if (y > max2) { max2 = y; } // third way // - use conditional operator max3 = x>y ? x : y; cout << "The maximum value is " << max1 << '\n'; cout << "The maximum value is " << max2 << '\n'; cout << "The maximum value is " << max3 << '\n'; cont(); return 0; } void cont() { if (cin.rdbuf()->sungetc() != -1 && cin.get() != '\n') cin.ignore(80,'\n'); cout << "Press enter to continue..."; cin.get(); }