1
0
LearningCPP/main.cpp

61 lines
1.5 KiB
C++
Raw Normal View History

2022-08-12 14:59:08 +01:00
#include <iostream>
2022-08-13 00:34:25 +01:00
2022-08-26 11:55:54 +01:00
using string = std::string;
void ignoreTillLineEnd();
string promptfs(string);
int promptfi(string);
2022-08-12 14:59:08 +01:00
int main()
{
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";
2022-08-26 11:55:54 +01:00
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();
ignoreTillLineEnd();
std::cout << "Input Failure!\n";
}
std::cout << msg << "\n";
std::cin >> toret;
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();
2022-08-26 11:55:54 +01:00
std::cout << "Input Failure!\n";
}
std::cout << msg << "\n";
std::cin >> toret;
once = true;
}
while (std::cin.fail());
ignoreTillLineEnd();
2022-08-26 11:55:54 +01:00
return toret;
2022-08-12 14:59:08 +01:00
}