/* ************************************************************************** * Program name : 057_Guess_an_number (Version 1.00) * * Author : Duck Wong * * Language : C / C++ * * Compiler : Boodshed Dec-C++ compiler Ver 3.95 * * Computer : PII350 * * O/S : Windows 98 * ************************************************************************** * Version 1.00 : 2000/11/29 - first version * ************************************************************************** * Description : (a) Guess an integer number that is selected by you. * * (b) User keyin the current status (C,H or L) * * (c) Until the computer find your number * * (d) Try again ? * ************************************************************************** */ #include #include #include #include int main() { // part 1 : declaration int maxval, minval, fir, cor, err; char resp, Again; srand(time(NULL)); do { cor=1; err=1; maxval=100; minval=1; cout << "\nNumber Guesser\n" << "\nPick a number between 1 and 100." << "\n-----------------------------------------------------------\n"; do { fir=(rand() % (maxval-minval+1))+minval; cout << "\ncurrent status : your number is greater than or equal to " << minval << "\n and also smaller than or equal to " << maxval; cout << "\n\nIs your number " << fir << " ?" << "\n\n(C)Correct, (L)too low or (H)too high] ? "; cin >> resp; if (resp=='c' || resp=='C') { cout << "\n\nYes! Your number is " << fir << "!"; cor++; } else if (resp=='h' || resp=='H') { maxval=fir-1; } else if (resp=='l' || resp=='L') { minval=fir+1; } else { cout << "\nYou did not enter a correct value/n."; cout << "\nPlease enter L/H/C."; } if (maxval == minval) { cout << "\n\nYes! I find it. Your number is " << minval << "!"; cor++; } else if (maxval < minval ) { cout << "\n\nDon't tell lie! " << "I know that your number should be " << fir; err++; } }while ((cor<2) & (err<2)); // part 4 : try another number ? cout << "\n\nTry another number (Y/N) : "; cin >> Again; cout << "\n"; } while (Again=='Y' || Again=='y'); cout << "\n" << endl; system("PAUSE"); return 0; }