1
0

Test loop and input error checking 2.

Fix issue with error checking.
This commit is contained in:
Captain ALM 2022-08-26 12:10:43 +01:00
parent 9c7d7772e6
commit a9b58871c0
Signed by: alfred
GPG Key ID: 4E4ADD02609997B1

View File

@ -3,11 +3,19 @@
using string = std::string; using string = std::string;
void ignoreTillLineEnd(); void ignoreTillLineEnd();
string promptfs(string); string promptfs(string);
int promptfi(string);
int main() int main()
{ {
string name {promptfs("Enter your name:")}; int nameC {promptfi("Enter the number of names:")};
std::cout << "Hello " << name << "\n"; if (nameC < 1) {
std::cout << "No names to be entered!\n";
return 1;
}
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";
return 0; return 0;
} }
@ -21,13 +29,32 @@ string promptfs(string msg) {
do { do {
if (once) { if (once) {
std::cin.clear(); std::cin.clear();
ignoreTillLineEnd();
std::cout << "Input Failure!\n"; std::cout << "Input Failure!\n";
} }
std::cout << msg << "\n"; std::cout << msg << "\n";
std::cin >> toret; std::cin >> toret;
ignoreTillLineEnd();
once = true; once = true;
} }
while (std::cin.fail()); while (std::cin.fail());
ignoreTillLineEnd();
return toret;
}
int promptfi(string msg) {
bool once {false};
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; return toret;
} }