Please send questions to
st10@humboldt.edu .
/*--------------------------------------------------
created by smtuttle at Mon Apr 19 14:44:07 PDT 2010
--------------------------------------------------*/
#include <iostream>
#include <cmath>
#include "guess.h"
using namespace std;
/*--------------------------------------------------
Contract: guess : void -> int
Purpose: expects nothing, flakily-randomly
chooses a number between 1 and 100, and asks the
user to guess what it is, until he/she does;
then it returns the number of guesses it took
Examples:
for guess()
...if it takes me 3 guesses to guess the number,
then guess() == 3
--------------------------------------------------*/
int guess()
{
int num_guesses = 0;
int target;
int guess;
// generate the target
srand( time(NULL) );
target = 1 + (rand() % MAX_POSSIBLE);
// get the first guess from the user
cout << "Enter an integer between 1 and "
<< MAX_POSSIBLE << ": ";
cin >> guess;
num_guesses++;
// make them keep trying until they get it
// right, counting the number of guesses
while (target != guess)
{
if (guess < target)
cout << "Pick a higher number..." << endl;
else
cout << "Pick a lower number..." << endl;
cout << "Enter your next guess: ";
cin >> guess;
num_guesses++;
}
cout << "YOU GOT IT!!" << endl;
return num_guesses;
}