fork download
  1. //Charlotte Davies-Kiernan CS1A Chapter 5 P.298 #20
  2. //
  3. /******************************************************************************
  4.  *
  5.  * Randomize Number Guessing
  6.  * ____________________________________________________________________________
  7.  * This program will generate a random number for the user to guess, the number
  8.  * being between 1 and 100.
  9.  * ____________________________________________________________________________
  10.  * Input
  11.  * guess //Guess the user makes between 1 and 100
  12.  * Output
  13.  * randomNumber //Random Number the program generates that the user is trying to guess
  14.  *****************************************************************************/
  15. #include <iostream>
  16. #include <cstdlib>
  17. #include <iomanip>
  18. using namespace std;
  19. int main()
  20. {
  21. int guess; //INPUT - guess the user makes to try and guess the number
  22. int randomNumber; //OUTPUT - the program will let the user know whether or not they guessed correctly
  23. //
  24. //Random Number Generator
  25. randomNumber = rand () % 100 + 1;
  26. guess = 0;
  27. //
  28. //Prompt User
  29. cout << "Guess the number (between 1 and 100): " << endl;
  30. //
  31. //Check if guess was correct
  32. while (guess != randomNumber){
  33. cin >> guess;
  34. if (guess < 1 || guess > 100){
  35. cout << "Out of range. Please enter a number 1-100." << endl;
  36. break;}
  37. if (guess < randomNumber)
  38. cout << "Too low try again!" << endl;
  39. else if (guess > randomNumber)
  40. cout << "Too high try again!" << endl;
  41. else
  42. cout << "Congratulations!! You've guessed the number!" << endl;
  43. }
  44. return 0;
  45. }
Success #stdin #stdout 0s 5316KB
stdin
1
45
55
75
99
97
69
101
stdout
Guess the number (between 1 and 100): 
Too low try again!
Too low try again!
Too low try again!
Too low try again!
Too high try again!
Too high try again!
Too low try again!
Out of range. Please enter a number 1-100.