Guest User

Untitled

a guest
Oct 22nd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. // Guess my number
  2. // The classic number guessing game
  3.  
  4. #include <iostream>
  5. #include <cstdlib>
  6. #include <ctime>
  7. #include <string>
  8.  
  9. using namespace std;
  10.  
  11. int main()
  12. {
  13. int tries = 0;
  14. string ans;
  15.  
  16. cout << "Think of a number...\n\n";
  17.  
  18. do
  19. {
  20. srand(static_cast<unsigned int>(time(0))); //seed
  21.  
  22. int secretNumber = rand()%100+1; //random number between 1 and 100
  23. cout << secretNumber << "\nIs this your number?\n(y/n)\n";
  24. cin >> ans;
  25. ++tries;
  26.  
  27. if ( ans == "y")
  28. {
  29. cout << "Hooray I won! And in only " << tries << " guesses!\n";
  30. }
  31.  
  32. if ( ans == "n")
  33.  
  34. {
  35. cout << "Then I will try again...\n\n";
  36. }
  37. }while (ans !="y");
  38.  
  39. return 0;
  40. }
Add Comment
Please, Sign In to add comment