From c1b6ffe70bd281c6c230fd63fabcaac2aff47514 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Sun, 7 Apr 2024 23:18:32 -0700 Subject: feat: initial commit --- chapter1/guess.cxx | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 chapter1/guess.cxx (limited to 'chapter1/guess.cxx') 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 // Provides assert +#include // Provides cout and cin +#include // 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; +} -- cgit v1.2.3