/*****************************************************************/
/** The Guess-A-Number
Game!! **/
/** **/
/** Programmer: Ryan
Richardson **/
/** **/
/** This program asks the
user to guess a number **/
/** between 1 and 10. What the user doesn't know, **/
/** however, is that the
program will always say **/
/** that the user picked
the wrong number. **/
/** **/
/*****************************************************************/
#include
<iostream.h> //
needed for cin and cout
const int range = 10;
bool guessedAlready[range
+ 1]; // this array keeps
track of what numbers the
//
the user has guessed already
int guess,i,sofar = 0;
int main()
{
for (i=0; i<=range; i++) // initializing the array to all false
{
guessedAlready[i] = false;
}
cout << "Hello. This is the Guess-A-Number game." << endl;
cout << "I am thinking of a number between 1
and " << range << "." << endl;
while (sofar < (range - 1)) // while the user still has some guesses left
{
cout << "Please guess a number
between 1 and " << range << endl;
cin >> guess;
if (guessedAlready[guess] == true)
{
cout << "You already
guessed that number." << endl;
continue;
}
if ((guess < 1) || (guess > 10))
{
cout << "You must
guess a number between 1 and " << range << "."
<< endl;
continue;
}
guessedAlready[guess] = true; // marking the guessed
number as taken
sofar++;
cout << endl << "Sorry,
" << guess << " is not correct. You have " <<
(range - sofar - 1
);
cout << " guesses left."
<< endl;
}
// finding and printing the only number that was not
guessed
for (i=1; (i < range) && (guessedAlready[i] ==
true); i++);
cout << "The correct number was "
<< i << endl;
return i; //
the unguessed number is returned to the OS
}