1
0
LearningCPP/main.cpp

72 lines
1.7 KiB
C++
Raw Normal View History

2022-08-12 14:59:08 +01:00
#include <iostream>
2022-08-26 13:58:55 +01:00
#include <random>
#include <chrono>
2022-08-13 00:34:25 +01:00
2022-08-26 11:55:54 +01:00
using string = std::string;
void ignoreTillLineEnd();
string promptfs(string);
int promptfi(string);
2022-08-12 14:59:08 +01:00
int main()
{
2022-08-26 13:58:55 +01:00
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) {
if (ent > num)
std::cout << "You Guessed Too High!\n";
else
std::cout << "You Guessed Too Low!\n";
}
2022-08-26 13:58:55 +01:00
ent = promptfi("Enter your guess:");
once = true;
}
2022-08-26 13:58:55 +01:00
while (num != ent);
std::cout << "Well done " << name << ", you guessed correctly!\n";
2022-08-26 11:55:54 +01:00
return 0;
}
void ignoreTillLineEnd() {
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
string promptfs(string msg) {
2022-08-26 13:58:55 +01:00
bool once {};
2022-08-26 11:55:54 +01:00
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) {
2022-08-26 13:58:55 +01:00
bool once {};
int toret {};
do {
if (once) {
std::cin.clear();
ignoreTillLineEnd();
2022-08-26 11:55:54 +01:00
std::cout << "Input Failure!\n";
}
std::cout << msg << "\n";
std::cin >> toret;
once = true;
}
while (std::cin.fail());
ignoreTillLineEnd();
2022-08-26 11:55:54 +01:00
return toret;
2022-08-12 14:59:08 +01:00
}