fork download
  1. //Sam Partovi CS1A Chapter 5, P 298, #20
  2. //
  3. /*******************************************************************************
  4. *
  5. * SIMULATE NUMBER GUESSING GAME
  6. * ____________________________________________________________
  7. * This program simulates a number guessing game with a randomly generated
  8. * integer with a specified maximum and minimum value.
  9. * ____________________________________________________________
  10. *INPUT
  11. * guess : Guessed number to be validated as correct or incorrect
  12. *
  13. *OUTPUT
  14. * randomNumber : A randomly generated number seeded by system time.
  15. ******************************************************************************/
  16. #include <iostream>
  17. #include <iomanip>
  18. #include <ctime>
  19. using namespace std;
  20. int main ()
  21. {
  22. int guess; //INPUT - Number to be validated against randomNumber
  23. int randomNumber; //INPUT - A randomly generated number seeded by system time.
  24.  
  25. //GENERATE A RANDOM NUMBER
  26. srand((int)time(NULL));
  27. randomNumber = rand() % 11;
  28.  
  29. //PROMPT FOR USER GUESS
  30. cout << "Guess the random number between 0 and 10: ";
  31. cin >> guess;
  32.  
  33. //VALIDATE GUESS RELATION TO RANDOM NUMBER
  34. while (true) {
  35. if (guess > randomNumber) {
  36. cout << "\nToo high, try again! ";
  37. }
  38. else if (guess == randomNumber) {
  39. cout << "\nCorrect! The number was " << randomNumber << ".\n";
  40. break;
  41. }
  42. else {
  43. cout << "\nToo low, try again! ";
  44. }
  45. cin >> guess;
  46. }
  47. return 0;
  48.  
  49. }
  50.  
Success #stdin #stdout 0.01s 5288KB
stdin
1
2
3
4
5
6
7
8
9
10
stdout
Guess the random number between 0 and 10: 
Too low, try again! 
Too low, try again! 
Too low, try again! 
Too low, try again! 
Correct! The number was 5.