1
0

Tutorial 1.

Question 1.
This commit is contained in:
Captain ALM 2022-10-07 12:10:31 +01:00
parent 78acaf3290
commit 3d2827578e
Signed by: alfred
GPG Key ID: 4E4ADD02609997B1

106
main.cpp
View File

@ -1,101 +1,15 @@
#include <iostream>
#include <random>
#include <chrono>
using string = std::string;
void ignoreTillLineEnd();
string promptfs(string);
int promptfi(string);
string guessRank(int);
int main()
{
std::mt19937 rand {std::random_device{}()};
std::uniform_int_distribution<int> clamp {1, 100};
string name {promptfs("Enter your name:")};
std::cout << "Hello " << name << ", welcome to guess the number.\n";
int guesses {};
int num {clamp(rand)}, ent {};
do {
if (guesses++ > 0) {
if (ent > num)
std::cout << "You Guessed Too High!\n";
else
std::cout << "You Guessed Too Low!\n";
}
ent = promptfi("Enter your guess:");
}
while (num != ent);
std::cout << "Well done " << name << ", you guessed correctly!\n";
std::cout << "You took " << guesses << " guesses, rank " << guessRank(guesses) << " .\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;
}
string guessRank(int g) {
switch (g) {
case 0:
return "U";
case 1:
return "A^";
case 2:
return "A*";
case 3:
return "A";
case 4:
case 5:
return "B";
case 6:
case 7:
return "C";
case 8:
case 9:
case 10:
return "D";
case 11:
case 12:
case 13:
return "E";
default:
return "F";
}
int main() {
std::cout << "Enter your age:\n";
int age {0};
std::cin >> age;
if (age < 18)
std::cout << age << " is too young.\n";
else if (age > 30)
std::cout << age << " is too old.\n";
else
std::cout << age << " is the right age.\n";
}