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;
void ignoreTillLineEnd();
string promptfs(string);
int promptfi(string);
int main()
{
string name {promptfs("Enter your name:")};
std::cout << "Hello " << name << "\n";
int nameC {promptfi("Enter the number of names:")};
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;
}
@ -21,13 +29,32 @@ string promptfs(string msg) {
do {
if (once) {
std::cin.clear();
ignoreTillLineEnd();
std::cout << "Input Failure!\n";
}
std::cout << msg << "\n";
std::cin >> toret;
ignoreTillLineEnd();
once = true;
}
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;
}