67 lines
1.6 KiB
C++
67 lines
1.6 KiB
C++
#include <iostream>
|
|
#include <random>
|
|
#include <chrono>
|
|
|
|
using string = std::string;
|
|
void ignoreTillLineEnd();
|
|
string promptfs(string);
|
|
int promptfi(string);
|
|
|
|
int main()
|
|
{
|
|
std::mt19937 rand {std::random_device{}()};
|
|
std::uniform_int_distribution clamp {1, 10};
|
|
string name {promptfs("Enter your name:")};
|
|
std::cout << "Hello " << name << ", welcome to guess the number.\n";
|
|
bool once {};
|
|
int num {clamp(rand)}, ent {};
|
|
do {
|
|
if (once) std::cout << "You Guessed Wrong!\n";
|
|
ent = promptfi("Enter your guess:");
|
|
once = true;
|
|
}
|
|
while (num != ent);
|
|
std::cout << "Well done " << name << ", you guessed correctly!\n";
|
|
return 0;
|
|
}
|
|
|
|
void ignoreTillLineEnd() {
|
|
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
|
|
}
|
|
|
|
string promptfs(string msg) {
|
|
bool once {};
|
|
string toret {};
|
|
do {
|
|
if (once) {
|
|
std::cin.clear();
|
|
ignoreTillLineEnd();
|
|
std::cout << "Input Failure!\n";
|
|
}
|
|
std::cout << msg << "\n";
|
|
std::cin >> toret;
|
|
once = true;
|
|
}
|
|
while (std::cin.fail());
|
|
ignoreTillLineEnd();
|
|
return toret;
|
|
}
|
|
|
|
int promptfi(string msg) {
|
|
bool once {};
|
|
int toret {};
|
|
do {
|
|
if (once) {
|
|
std::cin.clear();
|
|
ignoreTillLineEnd();
|
|
std::cout << "Input Failure!\n";
|
|
}
|
|
std::cout << msg << "\n";
|
|
std::cin >> toret;
|
|
once = true;
|
|
}
|
|
while (std::cin.fail());
|
|
ignoreTillLineEnd();
|
|
return toret;
|
|
}
|