// С использованием перегрузки оператора ввода
#include <iostream>
#include <limits>
#include <string>
using namespace std;
template<typename T>
T input(istream& inp, const char* msg) {
cout << msg;
T value;
inp >> value;
inp.ignore(numeric_limits<streamsize>::max(), '\n');
return value;
}
struct Person {
int age;
string name;
private:
friend istream& operator>>(istream& inp, Person& p) {
cout << "Ф. И. О.: ";
getline(inp, p.name);
p.age = input<int>(inp, "Возраст: ");
return inp;
}
};
int main() {
system("chcp 1251 > nul");
const auto length = 8U;
Person persons[length];
for (auto& person : persons) cin >> person;
system("pause > nul");
}
// С использованием функции
#include <iostream>
#include <limits>
#include <string>
using namespace std;
template<typename T>
T input(istream& inp, const char* msg) {
cout << msg;
T value;
inp >> value;
inp.ignore(numeric_limits<streamsize>::max(), '\n');
return value;
}
struct Person {
int age;
string name;
};
Person input_person() {
Person p;
cout << "Ф. И. О.: ";
getline(cin, p.name);
p.age = input<int>(cin, "Возраст: ");
return p;
}
int main() {
system("chcp 1251 > nul");
const auto length = 8U;
Person persons[length];
for (auto& person : persons) person = input_person();
system("pause > nul");
}
// С использованием метода
#include <iostream>
#include <limits>
#include <string>
using namespace std;
template<typename T>
T input(istream& inp, const char* msg) {
cout << msg;
T value;
inp >> value;
inp.ignore(numeric_limits<streamsize>::max(), '\n');
return value;
}
struct Person {
int age;
string name;
void set() {
cout << "Ф. И. О.: ";
getline(cin, name);
age = input<int>(cin, "Возраст: ");
}
};
int main() {
system("chcp 1251 > nul");
const auto length = 8U;
Person persons[length];
for (auto& person : persons) person.set();
system("pause > nul");
}