fork download
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. int tries = 1; // keeps count of how many guesses are made
  10. string playAgain;
  11. string theNumber;
  12.  
  13. cout << "\tGuessing Your Number!\n\n";
  14. cout << "Think of a number between 1 and 10 and I will guess which you're thinking of!\n";
  15.  
  16. do
  17. {
  18. srand(static_cast<unsigned int>(time(NULL))); //seeding random number generator
  19. int guessingNumber = rand() % 10 + 1; // generates a random number between 1 and 10
  20. cout << "Is it " << guessingNumber << "? y/n ";
  21. cin >> theNumber;
  22.  
  23. if (theNumber == "y")
  24. {
  25. cout << "Yay! I guessed your number! \nIt only took me " << tries << "tries! \nDo you want to play again? y/n";
  26. cin >> playAgain;
  27. }
  28. else {
  29. tries++;
  30. continue;
  31. }
  32. }while (playAgain == "y");
  33.  
  34. cout << "I hope you had fun!\n\n";
  35. }
  36.  
Success #stdin #stdout 0s 3344KB
stdin
Standard input is empty
stdout
	Guessing Your Number!

Think of a number between 1 and 10 and I will guess which you're thinking of!
Is it 8? y/n I hope you had fun!