diff options
Diffstat (limited to 'chapter1/guess.cxx')
| -rw-r--r-- | chapter1/guess.cxx | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/chapter1/guess.cxx b/chapter1/guess.cxx new file mode 100644 index 0000000..899c3bc --- /dev/null +++ b/chapter1/guess.cxx @@ -0,0 +1,43 @@ +// FILE: guess.cxx
+// Demostrates a guessing game function that's used as a time analysis example.
+
+#include <cassert> // Provides assert
+#include <iostream> // Provides cout and cin
+#include <cstdlib> // Provides EXIT_SUCCESS
+using namespace std; // Allows all Standard Library items to be used
+
+// Prototype for the function used in this demonstration program
+void guess_game(int n);
+// Precondition: n > 0.
+// Postcondition: The user has been asked to think of a number between 1 and n.
+// The function asks a series of questions, until the number is found.
+
+
+int main( )
+{
+ guess_game(100);
+ return EXIT_SUCCESS;
+}
+
+void guess_game(int n)
+// Library facilities used: cassert, iostream
+{
+ int guess;
+ char answer;
+
+ assert(n >= 1);
+
+ cout << "Think of a whole number from 1 to " << n << "." << endl;
+ answer = 'N';
+ for (guess = n; (guess > 0) && (answer != 'Y') && (answer != 'y'); --guess)
+ {
+ cout << "Is your number " << guess << "?" << endl;
+ cout << "Please answer Y or N, and press return: ";
+ cin >> answer;
+ }
+
+ if ((answer == 'Y') || (answer == 'y'))
+ cout << "I knew it all along." << endl;
+ else
+ cout << "I think you are cheating!" << endl;
+}
|