Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Guess my number
- // The classic number guessing game
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- #include <string>
- using namespace std;
- int main()
- {
- int tries = 0;
- string ans;
- cout << "Think of a number...\n\n";
- do
- {
- srand(static_cast<unsigned int>(time(0))); //seed
- int secretNumber = rand()%100+1; //random number between 1 and 100
- cout << secretNumber << "\nIs this your number?\n(y/n)\n";
- cin >> ans;
- ++tries;
- if ( ans == "y")
- {
- cout << "Hooray I won! And in only " << tries << " guesses!\n";
- }
- if ( ans == "n")
- {
- cout << "Then I will try again...\n\n";
- }
- }while (ans !="y");
- return 0;
- }
Add Comment
Please, Sign In to add comment