#include <iostream>
#include <string>
#include <limits>
void simple_name(std::string& first_name, std::string& last_name);
void complex_name(std::string& first_name, std::string& last_name);
int main() {
std::string first, last;
std::cout << "Simple method.\n";
simple_name(first, last);
std::cout << "\nYour name (simple method): " << first << " " << last << std::endl;
std::cout << "\nComplex method.\n";
complex_name(first, last);
std::cout << "\nYour name (complex method): " << first << " " << last << std::endl;
}
void simple_name(std::string& first_name, std::string& last_name) {
char first_operator, second_operator;
std::cout << "Please enter the first seperation operator :";
std::cin >> first_operator; // Get first character from input.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Ignore the rest.
std::cout << "Please enter the second seperation operator :";
std::cin >> second_operator; // Get first character from input.
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // Ignore the rest.
std::cout << "Please enter your name:";
std::getline(std::cin, first_name, first_operator );
std::getline(std::cin, last_name, second_operator );
std::cin.ignore();
}
void complex_name(std::string& first_name, std::string& last_name) {
std::string first_operator, second_operator;
std::cout << "Please enter the first seperation operator :";
std::getline(std::cin, first_operator);
std::cout << "Please enter the second seperation operator :";
std::getline(std::cin, second_operator);
bool done = false;
do {
std::string name_buf;
std::cout << "Please enter your name:";
std::getline(std::cin, name_buf);
auto first_op = name_buf.find(first_operator);
auto second_op = name_buf.find(second_operator);
if (first_op == std::string::npos || second_op == std::string::npos) {
std::cout << "Please separate your first and last names with the first specified operator, "
<< "and follow your last name with the second specified operator.\n";
continue;
}
first_name = name_buf.substr(0, first_op);
auto last_start = first_op + first_operator.size();
last_name = name_buf.substr(last_start, second_op - last_start);
done = true;
} while(!done);
}