From 9c7d7772e676410bca2cf0ce4e202171e7cbf387 Mon Sep 17 00:00:00 2001 From: Captain ALM Date: Fri, 26 Aug 2022 11:55:54 +0100 Subject: [PATCH] Test loop and input error checking 1. --- main.cpp | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/main.cpp b/main.cpp index 7e83ef8..33a48c8 100644 --- a/main.cpp +++ b/main.cpp @@ -1,11 +1,33 @@ #include -std::string* test() { - static std::string tr {"test"}; - return &tr; -} +using string = std::string; +void ignoreTillLineEnd(); +string promptfs(string); int main() { - std::cout << *test(); + string name {promptfs("Enter your name:")}; + std::cout << "Hello " << name << "\n"; + return 0; +} + +void ignoreTillLineEnd() { + std::cin.ignore(std::numeric_limits::max(), '\n'); +} + +string promptfs(string msg) { + bool once {false}; + string toret {}; + do { + if (once) { + std::cin.clear(); + std::cout << "Input Failure!\n"; + } + std::cout << msg << "\n"; + std::cin >> toret; + ignoreTillLineEnd(); + once = true; + } + while (std::cin.fail()); + return toret; }