home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C Programming Starter Kit 2.0
/
SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso
/
tybc4
/
game2.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1993-04-28
|
1KB
|
76 lines
#include <stdlib.h>
#include <iostream.h>
#include <time.h>
enum boolean { false, true };
// declare a global random number generating function
int random(int maxVal)
{ return rand() % maxVal; }
class game
{
protected:
int n;
int m;
int MaxIter;
int iter;
boolean ok;
void prompt();
void examineInput();
public:
game();
void play();
};
game::game()
{
MaxIter = 11;
iter = 0;
ok = true;
// reseed random-number generator
srand((unsigned)time(NULL));
n = random(1001);
m = -1;
}
void game::prompt()
{
cout << "Enter a number between 0 and 1000 : ";
cin >> m;
ok = (m < 0) ? false : true;
}
void game::examineInput()
{
// is the user's guess higher?
if (m > n)
cout << "Enter a lower guess\n\n";
else if (m < n)
cout << "Enter a higher guess\n\n";
else
cout << "You guessed it! Congratulations.";
}
void game::play()
{
// loop to obtain the other guesses
while (m != n && iter < MaxIter && ok) {
prompt();
iter++;
examineInput();
}
// did the user guess the secret number
if (iter >= MaxIter || ok == 0)
cout << "The secret number is " << n << "\n";
}
main()
{
game g;
g.play();
return 0;
}