1
0

Test loop and input error checking 1.

This commit is contained in:
Captain ALM 2022-08-26 11:55:54 +01:00
parent fffcdedae3
commit 9c7d7772e6
Signed by: alfred
GPG Key ID: 4E4ADD02609997B1

View File

@ -1,11 +1,33 @@
#include <iostream>
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<std::streamsize>::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;
}