home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C Programming Starter Kit 2.0
/
SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso
/
tybc4
/
game1.cpp
< prev
next >
Wrap
C/C++ Source or Header
|
1993-04-28
|
935b
|
43 lines
#include <stdlib.h>
#include <iostream.h>
#include <time.h>
// declare a global random number generating function
int random(int maxVal)
{ return rand() % maxVal; }
main()
{
int n, m;
int MaxIter = 11;
int iter = 0;
int ok = 1;
// reseed random-number generator
srand((unsigned)time(NULL));
n = random(1001);
m = -1;
// loop to obtain the other guesses
while (m != n && iter < MaxIter && ok == 1) {
cout << "Enter a number between 0 and 1000 : ";
cin >> m;
ok = (m < 0) ? 0 : 1;
iter++;
// 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.";
}
// did the user guess the secret number
if (iter >= MaxIter || ok == 0)
cout << "The secret number is " << n << "\n";
return 0;
}