1
0

Compare commits

..

No commits in common. "18c5f972c245eac65a5f7a727e6c912b51e3d5ea" and "78acaf3290d0a051c8034b496697bb2ab4436abf" have entirely different histories.

115
main.cpp
View File

@ -1,30 +1,101 @@
#include <iostream> #include <iostream>
#include <cstdlib> #include <random>
#include <ctime> #include <chrono>
using string = std::string;
void ignoreTillLineEnd();
string promptfs(string);
int promptfi(string);
string guessRank(int);
int main() int main()
{ {
using std::cout; std::mt19937 rand {std::random_device{}()};
using std::cin; std::uniform_int_distribution<int> clamp {1, 100};
srand(time(nullptr)); string name {promptfs("Enter your name:")};
int gcount{ 1 }; std::cout << "Hello " << name << ", welcome to guess the number.\n";
int n { 20 }; int guesses {};
int secret { rand()%n + 1 }; int num {clamp(rand)}, ent {};
cout << "Guess a number between 1 and " << n << ": "; do {
int guess; if (guesses++ > 0) {
cin >> guess; if (ent > num)
while (guess != secret) std::cout << "You Guessed Too High!\n";
{ else
if (guess > secret) { std::cout << "You Guessed Too Low!\n";
cout << "Number too big ; ";
} else {
cout << "Number too small ; ";
} }
gcount++; ent = promptfi("Enter your guess:");
cout << "Guess again: ";
cin >> guess;
} }
cout << "Correct!\n"; while (num != ent);
cout << "Took " << gcount << " tries.\n"; std::cout << "Well done " << name << ", you guessed correctly!\n";
std::cout << "You took " << guesses << " guesses, rank " << guessRank(guesses) << " .\n";
return 0; 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";
}
}