From cc4d91adf27d4437d1adce50e9705929886322c6 Mon Sep 17 00:00:00 2001 From: Captain ALM Date: Fri, 26 Aug 2022 13:58:55 +0100 Subject: [PATCH] Guess the number. --- main.cpp | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/main.cpp b/main.cpp index cb2464a..9149cf8 100644 --- a/main.cpp +++ b/main.cpp @@ -1,4 +1,6 @@ #include +#include +#include using string = std::string; void ignoreTillLineEnd(); @@ -7,15 +9,19 @@ int promptfi(string); int main() { - int nameC {promptfi("Enter the number of names:")}; - if (nameC < 1) { - std::cout << "No names to be entered!\n"; - return 1; + 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; } - string* names = new string[nameC]; - for (int i = 0; i < nameC; i++) names[i] = promptfs("Enter your name:"); - std::cout << "I have " << nameC << " to say hello to!\n"; - for (int i = 0; i < nameC; i++) std::cout << "Hello " << names[i] << ".\n"; + while (num != ent); + std::cout << "Well done " << name << ", you guessed correctly!\n"; return 0; } @@ -24,7 +30,7 @@ void ignoreTillLineEnd() { } string promptfs(string msg) { - bool once {false}; + bool once {}; string toret {}; do { if (once) { @@ -42,7 +48,7 @@ string promptfs(string msg) { } int promptfi(string msg) { - bool once {false}; + bool once {}; int toret {}; do { if (once) {