#include <iostream>
#include <random>
using namespace std;
class Guess {
public:
using value_t = unsigned;
Guess() : left(1), right(100) {}
void pause() const {
cin.ignore(0x1000, '\n');
cin.get();
}
void game() const {
uniform_int_distribution<value_t> uid(left, right);
mt19937 gen{ random_device()() };
const auto random_number = uid(gen);
cout << ">>> ";
value_t number;
cin >> number;
size_t attempt = 1;
while (number != random_number) {
cout << "<<< ";
puts(number < random_number ? "less" : "more");
cout << ">>> ";
cin >> number;
++attempt;
}
cout << "Guess in " << attempt << " tries. ";
pause();
}
private:
value_t left;
value_t right;
};
int main() {
Guess guess;
guess.game();
puts("Game over!");
guess.pause();
}
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand(static_cast<unsigned int>(time(0)));
int secretNumber = rand() % 100 + 1;
int tries = 0;
int guess;
cout << "\tWelcome to Guess My Number\n\n";
do {
cout << "Enter a guess: ";
cin >> guess;
tries++;
if (guess > secretNumber) {
cout << "Too high!\n\n";
}
else if (guess < secretNumber) {
cout << "Too low!\n\n";
}
else {
cout << "\nThat's it! You got it in " << tries << " guesses!\n";
}
} while (guess != secretNumber);
return 0;
}
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(static_cast<unsigned int>(time(0)));
int secretNumber = rand() % 100 + 1, tries = 0, guess;
cout << "\tWelcome to Guess My Number\n\n";
do
{
cout << "Enter a guess: ";
cin >> guess;
cout << ((++tries, guess > secretNumber) ? "Too high!\n\n" : (guess < secretNumber) ? "Too low!\n\n" : "\nThat's it! You got it in " + to_string(tries) + " guesses!\n");
} while (guess != secretNumber);
return 0;
}
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(static_cast<unsigned int>(time(0)));
int secretNumber = rand() % 100 + 1;
int tries = 0;
int guess;
cout << "\tWelcome to Guess My Number\n\n";
}
do
{
cout << "Enter a guess: ";
cin >> guess;
++tries;
if (guess > secretNumber);
{
cout << "Too high!\n\n";
}
else if (guess < secretNumber);
{
cout << "Too low!\n\n";
}
else
{
cout << "\nThat's it! You got it in" << tries << "guesses!\n";
}
return 0;
} while (guess != secretNumber);