Let's look at this program :
// file : guess.C
#include <iostream.h>
const int tries=6;
main(int argc, char *argv[])
{
int num;
int guess;
int i;
srand(atoi(argv[1]));
num=rand() % 128 + 1;
cout << "I'm thinking of a number between 1 and 128" << endl;
cout << "You have " << tries << " guesses." << endl;
for (i=0 ; i<tries ; i++)
{
cout << "(" << i+1 << ") Take a guess : ";
cin >> guess;
if (guess > num)
cout << "Nope, my number is smaller than that." << endl;
else if (guess < num)
cout << "Nope, my number is larger than that." << endl;
else
{
cout << "That's it!" << endl;
break;
}
}
if (i==tries)
cout << "Sorry, you used up all your guesses..." << endl;
}
There are a couple of new things here :
Can you modify this program to use a while loop or a do-while loop instead of a for loop?
Why do you think I set tries = 6 and made the numbers range between 1 and 128?